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 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 蓝桥杯单片机第十三届第一场,整点继电器吸合,5s后断开出现了问题