简体   繁体   中英

Select all from another table in select query

I have two tables:

reviews

  • company_id (column)

employees

  • company_id (column)
  • email (column)

i have many reviews and many employees.

I have a select like this:

$stmt = "SELECT * FROM reviews WHERE user_id = :user_id";

My question: In the same select query, how can i select all (multiple) the employees's email using the company id from the reviews table?

You need to JOIN the two tables

SELECT * 
FROM reviews
JOIN employees ON reviews.company_id = reviews.company_id
WHERE user_id = :user_id

SELECT * here will return all the columns of both the reviews and employees tables.

Why do you want using company_id from reviews table when employees table already has it. Anyway You can join 2 table together to resolve your issue. Like this:

SELECT email 
FROM employees e JOIN reviews r ON e.company_id = r.company_id 
WHERE r.company_id = :company_ud

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