PHP MySQL从一个表中选择ID,从另一个表中选择信息

时间:2022-09-26 10:52:37

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:

我有两个表,一个表称为queuelist,另一个表是呼叫信息。在queuelist表中,它只列出了不同的ID。我试图从该表中获取'clientID'并将其与包含所有信息的另一个表中的'ID'匹配,并将其显示在页面上。以下是表格的外观:

Table - queuelist

表 - queuelist

ID | clientID
-------------
1  | 589
2  | 254
3  | 486

Table - info

表 - 信息

ID   | Name  | Phone
--------------------
256  | Bob   | 5551231234
486  | Jack  | 5551231234
589  | Jill  | 5551231234

4 个解决方案

#1


6  

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
);

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.

我在上面的查询中使用别名用于较短的表示法(queuelist变为q而信息变为i)然后将连接条件(ON()之间的位设置为来自queuelist表的clientID应该匹配中的ID信息表。

Also see http://dev.mysql.com/doc/refman/5.0/en/join.html for more details.

另请参阅http://dev.mysql.com/doc/refman/5.0/en/join.html以获取更多详细信息。

#2


4  

You need to use an inner join

您需要使用内部联接

  select * from queuelist as ql inner join info as i on ql.clientID = i.ID

Though you might want to replace * with specific field names e.g

虽然您可能希望用特定的字段名称替换*,例如

  select ql.clientID, i.fieldname FROM....

#3


1  

Well, I see no difficulty in this using a JOIN.

好吧,我认为使用JOIN没有任何困难。

SELECT * FROM queuelist JOIN info ON clientID = info.ID WHERE queuelist.ID = 2

#4


0  

"Where" would be another option.

“哪里”将是另一种选择。

SELECT Name, Phone FROM queuelist,info WHERE clientID = ID

Assuming you want only name and phone

假设你只想要名字和电话

#1


6  

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
);

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.

我在上面的查询中使用别名用于较短的表示法(queuelist变为q而信息变为i)然后将连接条件(ON()之间的位设置为来自queuelist表的clientID应该匹配中的ID信息表。

Also see http://dev.mysql.com/doc/refman/5.0/en/join.html for more details.

另请参阅http://dev.mysql.com/doc/refman/5.0/en/join.html以获取更多详细信息。

#2


4  

You need to use an inner join

您需要使用内部联接

  select * from queuelist as ql inner join info as i on ql.clientID = i.ID

Though you might want to replace * with specific field names e.g

虽然您可能希望用特定的字段名称替换*,例如

  select ql.clientID, i.fieldname FROM....

#3


1  

Well, I see no difficulty in this using a JOIN.

好吧,我认为使用JOIN没有任何困难。

SELECT * FROM queuelist JOIN info ON clientID = info.ID WHERE queuelist.ID = 2

#4


0  

"Where" would be another option.

“哪里”将是另一种选择。

SELECT Name, Phone FROM queuelist,info WHERE clientID = ID

Assuming you want only name and phone

假设你只想要名字和电话