A
A
anton_myaso2021-06-19 23:01:22
SQL
anton_myaso, 2021-06-19 23:01:22

How to make SQL query from multiple tables?

help to make request:
There are 2 tables: users, orders.
users: id, name, s_name, email
orders: product_name, product_id, user_id
Select: Name, S_name, email ('users') by user_id and matching product_id (orders)

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Slava Rozhnev, 2021-06-20
@anton_myaso

You can use the WHERE ... EXISTS (SELECT ...)

select name, s_name, email
from users
where users.id = 1 and exists (
  select 1 from orders where user_id = users.id and product_id = 2
);

or JOIN
select distinct name, s_name, email
from users
join orders on orders.user_id = users.id
where users.id = 1 and product_id = 2;

MySQL playground

D
Developer, 2021-06-19
@samodum

there is a JOIN ... ON for that

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question