dongrong6235 2015-09-21 15:32
浏览 64
已采纳

使用php变量查询语法以从两个MySQL表中进行选择

I currently have a family tree database. I get the data to display by doing:

$sql = "SELECT * FROM persons WHERE ref='$personID'";

This works nicely. However, I've recently added another table called census_data. I'm trying to query this table at the same time as the persons table without success. I've tried doing this:

$sql = "SELECT * FROM persons, census_data 
WHERE persons.$personID = census_data.$personID ";

The error I get says:

Unknown column 'persons.405' in 'where clause'.

I'm far from competent with databases, but this error seems to be talking about columns, while it is a row that I'm looking for. Obviously I've messed up.

How do I do this?

  • 写回答

1条回答 默认 最新

  • douchenchepan6465 2015-09-21 15:36
    关注

    You need to join persons and census_data and also limit data by the personID:

    $sql = "SELECT * FROM persons, census_data 
    WHERE persons.ref = '$personID' AND persons.ref = census_data.person_id_column_name";
    

    The way you have it the query is looking for a column name equal to your personId (405 in your example) instead of looking for a personId of 405.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 隐藏系统界面pdf的打印、下载按钮
  • ¥15 MATLAB联合adams仿真卡死如何解决(代码模型无问题)
  • ¥15 基于pso参数优化的LightGBM分类模型
  • ¥15 安装Paddleocr时报错无法解决
  • ¥15 python中transformers可以正常下载,但是没有办法使用pipeline
  • ¥50 分布式追踪trace异常问题
  • ¥15 人在外地出差,速帮一点点
  • ¥15 如何使用canvas在图片上进行如下的标注,以下代码不起作用,如何修改
  • ¥50 vue router 动态路由问题
  • ¥15 关于#.net#的问题:End Function