MySQL retrieve 2 values from different tables
I didn't know how to explain it in so few words as to fit it in the title. However it's not really complicated. I have 2 tables, the first called "Lines" has these 2 columns:
Text PID
and the second called Poems these 2:
PID Title
I need to SELECT rows from the first table when they are equal to $something and then look up on the table Poems for the matching row (the one that has the same PID) and display both the Text from the first table and the Title from the second.
How can this be 开发者_StackOverflow社区done? Thanks
SELECT Lines.*, Poems.* FROM Lines
INNER JOIN Poems ON(Poems.PID = Lines.PID)
WHERE Lines.Text = 'SOME VALUE'
SELECT l.Text, p.Title
FROM lines l
INNER JOIN poems p
ON l.PID = p.PID
WHERE l.text = 'something'
You are looking for an Inner Join SQL query:
SELECT column_name(s) FROM table_name1 INNER JOIN
table_name2 ON table_name1.column_name=table_name2.column_name
WHERE table_namex.column_n = "xyz";
Which implies that in you case you will use:
SELECT Lines.*, Poems.*
FROM Lines l INNER JOIN Poems p
ON l.PID = p.PID
WHERE l.text = "xyz"
精彩评论