Home » Php » PHP MySQL Select ID from one table and information from another table

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

Posted by: admin July 12, 2020 Leave a comment

Questions:

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
How to&Answers:

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.

Answer:

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....

Answer:

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

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

Answer:

“Where” would be another option.

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

Assuming you want only name and phone