mysql - Writing a SQL query that gets information from 3 different tables -
i cant seem figure out how write these 2 queries on tables have created. 2 queries trying write
find users have reviewed both shops , restaurants. find users reviewed businesses, not shops or restaurants.
the tables using
reviews; +-------------+---------+------+-----+---------+-------+ | field | type | null | key | default | | +-------------+---------+------+-----+---------+-------+ | business_id | int(11) | no | pri | null | | | user_id | int(11) | no | pri | null | | | review_id | int(11) | no | pri | null | | | review_date | date | yes | | null | | | star_rating | int(1) | yes | | 1 | businesses +--------------+--------------+------+-----+---------+-------+ | field | type | null | key | default | | +--------------+--------------+------+-----+---------+-------+ | business_id | int(11) | no | pri | null | | | name | varchar(50) | yes | | null | | | city | varchar(40) | yes | | null | | | state | varchar(20) | yes | | null | | | full_address | varchar(120) | yes | | null | | users; +------------+-------------+------+-----+---------+-------+ | field | type | null | key | default | | +------------+-------------+------+-----+---------+-------+ | user_id | int(11) | no | pri | null | | | name | varchar(50) | yes | | null | | | user_since | date | yes | | null explain is_a_restaurant; +--------------+-------------+------+-----+---------+-------+ | field | type | null | key | default | | +--------------+-------------+------+-----+---------+-------+ | business_id | int(11) | no | pri | null | | | cuisine_type | varchar(20) | yes | | null | | | total_seats | int(11) | yes | | 1 | | +--------------+-------------+------+-----+---------+-------+ explain is_a_shop; +-------------+-------------+------+-----+---------+-------+ | field | type | null | key | default | | +-------------+-------------+------+-----+---------+-------+ | business_id | int(11) | no | pri | null | | | shop_type | varchar(50) | yes | | null | |
i dont know start. know going join businesses business id dont know how going find people reviewed both shops , restaurants. can help?
edit: have tried
for first query: find users have reviewed both shops , restaurants.
select b.business_id is_a_shop b join reviews r on r.business_id = b.business_id join is_a_restaurant k on r.business_id = k.business_id;
here's first 1 should like: can change select statement based on info want see.
select u.name users u inner join reviews rev on rev.user_id = u.user_id exists (select * is_a_shop s s.business_id = rev.business_id) or exists (select * is_a_restaurant r r.business_id = rev.business_id)
this pulls users wrote reviews business_id found in is_a_shop table or is_a_restaurant. should able figure out second query this.
Comments
Post a Comment