编写一个从3个不同表中获取信息的SQL查询

时间:2015-03-16 22:42:22

标签: mysql sql database

我似乎无法弄清楚如何在我创建的表上编写这两个查询。我想写的两个查询是

Find users that have reviewed both shops and restaurants.

Find users that reviewed businesses, but not shops or restaurants.

我正在使用的表是

 reviews;
+-------------+---------+------+-----+---------+-------+
| Field       | Type    | Null | Key | Default | Extra |
+-------------+---------+------+-----+---------+-------+
| 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 | Extra |
+--------------+--------------+------+-----+---------+-------+
| 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 | Extra |
+------------+-------------+------+-----+---------+-------+
| 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 | Extra |   
+--------------+-------------+------+-----+---------+-------+
| 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 | Extra |
 +-------------+-------------+------+-----+---------+-------+
 | business_id | int(11)     | NO   | PRI | NULL    |       |
 | shop_type   | varchar(50) | YES  |     | NULL    |       |

老实说,我不知道从哪里开始。我知道我将加入商业ID的企业,但我不知道我将如何找到审查商店和餐馆的人。有人可以帮忙吗?

编辑:我试过什么

对于第一个查询:查找已查看商店和餐馆的用户。

 SELECT b.business_id
 FROM 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;

2 个答案:

答案 0 :(得分:0)

以下是第一个应该是什么样的:您可以根据您想要查看的信息更改select语句。

SELECT u.name
FROM users u
INNER JOIN reviews rev ON rev.user_id = u.user_ID

WHERE EXISTS
(SELECT *
FROM is_a_shop s
WHERE s.business_id = rev.business_id)

OR EXISTS
(SELECT *
FROM is_a_restaurant r
WHERE r.business_id = rev.business_id)

这基本上拉动了所有撰写评论的用户,其中在is_a_shop表或is_a_restaurant中找到了business_id。你应该可以从中轻松找出第二个查询。

答案 1 :(得分:0)

与大多数SQL一样,这些查询可以通过多种不同方式解决。这些解决方案应该很容易理解:

查找已查看商店和餐馆的用户。

-- solution 1 - using joins:
select u.name 
from users u
join (
    select r.user_id 
    from reviews r 
    join is_a_restaurant i on i.business_id = r.business_id 
) resturant_reviews on u.user_id = resturant_reviews.user_id
join ( 
    select r.user_id 
    from reviews r 
    join is_a_shop i on i.business_id = r.business_id 
) shop_reviews on u.user_id = shop_reviews.user_id

-- solution 2: using exists - probably faster than solution 1
select u.name 
from users u
join reviews r on u.user_id = r.user_id
where exists (
    select 1 from is_a_restaurant i 
    where i.business_id = r.business_id 
) and exists (
    select 1 from is_a_shop i 
    where i.business_id = r.business_id 
)

查找审核商家的用户,而不是商店或餐馆。

-- solution 1: using not in - probably faster than solution 2
select u.name from users u
join reviews r on u.user_id = r.user_id
where r.business_id not in 
    (
    select business_id from is_a_restaurant
    union all 
    select business_id from is_a_shop
    )

-- solution 2: using exists
select u.name from users u
join reviews r on u.user_id = r.user_id
where not exists (
    select business_id from is_a_restaurant 
    where r.business_id = business_id
    )
and not exists (
    select business_id from is_a_shop 
    where r.business_id = business_id
    )