简体   繁体   中英

PHP MySQL Select ID from one table and information from another table

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:

Table - queuelist

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

Table - info

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

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.

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

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 eg

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

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

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

"Where" would be another option.

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

Assuming you want only name and phone

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM