austinh
austinh

Reputation: 1071

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

Upvotes: 7

Views: 16421

Answers (4)

TommyBs
TommyBs

Reputation: 9646

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

Upvotes: 4

Darvex
Darvex

Reputation: 3644

"Where" would be another option.

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

Assuming you want only name and phone

Upvotes: 0

Oldskool
Oldskool

Reputation: 34837

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.

Upvotes: 6

mauris
mauris

Reputation: 43619

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

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

Upvotes: 1

Related Questions