简体   繁体   中英

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:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM