开发者

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

开发者 https://www.devze.com 2023-02-23 07:33 出处:网络
I want to get data from two tables in my database. Here\'s what my tables might look like: table 1 with fields id, author, profession, country

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

  • table 1 with fields id, author, profession, country
  • table 2 with fields id, quote, author, category

I want to select quote and author from table 2 and the corresponding professio开发者_如何学Cn from table 1, with the same author in both tables.

How do I construct a query that does this?


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


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号