问题描述
我有两个表,一个表称为队列列表,另一个表称为呼叫信息.在队列列表中,它仅列出了不同的ID.我正在尝试从该表中获取"clientID",并将其与包含所有信息的另一个表中的"ID"进行匹配,并将其显示在页面上.表格外观如下:
I have two tables, one table is called queuelist and the other is call info. In the queuelist table it just lists different IDs. I am trying to get the 'clientID' from that table and match it with the 'ID' in the other table that contains all of the info and display it back on the page. Here is how the tables look:
表格-队列列表
ID | clientID
-------------
1 | 589
2 | 254
3 | 486
表格-信息
ID | Name | Phone
--------------------
256 | Bob | 5551231234
486 | Jack | 5551231234
589 | Jill | 5551231234
推荐答案
这就是他们所说的联接表,您应该使用这样的查询:
This is what they call joining tables, you should use a query like this:
SELECT i.ID, i.Name, i.Phone FROM `queuelist` AS q
LEFT JOIN `info` AS i ON (
q.clientID = i.ID
);
我在上面的查询中使用别名来表示较短的符号(队列列表变为q,信息变为i),然后将联接条件(ON()之间的位)设置为队列列表中的clientID,应与信息表中的ID.
I'm using aliases for shorter notation in the above query (queuelist becomes q and info becomes i) and then set the join condition (the bit between the ON()) to be the clientID from the queuelist table should match the ID in the info table.
另请参见 http://dev.mysql.com/doc/refman/5.0/en/join.html 了解更多详情.
Also see http://dev.mysql.com/doc/refman/5.0/en/join.html for more details.
这篇关于PHP MySQL从一个表中选择ID,从另一个表中选择信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!