IberoMedia
IberoMedia

Reputation: 2304

How can I get data from from two different tables in a single MySQL query?

I want to get data from two tables in my database. Here's what my tables might look like:

I want to select quote and author from table 2 and the corresponding profession from table 1, with the same author in both tables.

How do I construct a query that does this?

Upvotes: 1

Views: 936

Answers (3)

Pifon
Pifon

Reputation: 356

SELECT table2.quote, table2.author, table1.profession FROM table2, table1 WHERE table2.author=table1.author

you can add LIMIT 1 at the end to have single result.

Upvotes: 0

Mahesh
Mahesh

Reputation: 34625

select T2.quote, T2.author, T1.profession
   from table1 T1, tabel2 T2
   where T1.id = T2.id

Upvotes: 0

Maxime Pacary
Maxime Pacary

Reputation: 23041

Supposing that your author column contains unique identifiers for authors, try:

SELECT t2.quote, t2.author, t1.profession
FROM table2 t2
  LEFT JOIN table1 t1 ON t2.author = t1.author

Upvotes: 4

Related Questions