在 LEFT JOIN 查询中包含第三个表
我有五个 mysql 表。
shops
+----+--------------+--------------+ | id | name | address | +----+--------------+--------------+ | 1 | Shop1 | Street1 | | 2 | Shop2 | Street2 | | 3 | Shop3 | Street3 | | 4 | Shop4 | Street4 | +----+--------------+--------------+
fruits
+----+--------------+--------------+ | id | fruit | price | +----+--------------+--------------+ | 1 | Bannana | 2.5 | | 2 | Apple | 2.1 | | 3 | Orange | 1.8 | | 4 | Plum | 2.2 | +----+--------------+--------------+
availability
+----+--------------+--------------+ | id | shop_id | fruit_id | +----+--------------+--------------+ | 1 | 1 | 2 | | 2 | 2 | 2 | | 3 | 1 | 3 | | 4 | 2 | 1 | +----+--------------+--------------+
shop_activity
+----+--------------+--------------+--------------+ | id | shop_id | user_id | status | +----+--------------+--------------+--------------+ | 1 | 2 | 1 | 1 | | 2 | 3 | 2 | 1 | | 3 | 1 | 2 | 2 | | 4 | 2 | 2 | 1 | +----+--------------+--------------+--------------+
users
+----+--------------+ | id | name | +----+--------------+ | 1 | Peter | | 2 | John | +----+--------------+
I有查询
SELECT
availability.shop_id,
shops.name
FROM availability
LEFT JOIN shops
ON availability.shop_id=shops.id
WHERE
fruit_id = 2
结果我得到了 id 为 2(苹果)的水果可用的商店名称列表。
如果 users
.id
= 1 在正确的商店旁边,我应该怎么做才能在查询中包含 shop_activity 表来获取用户的状态。像这样的事情......
Shop1, NULL Shop2, status: 1
I have five mysql tables.
shops
+----+--------------+--------------+ | id | name | address | +----+--------------+--------------+ | 1 | Shop1 | Street1 | | 2 | Shop2 | Street2 | | 3 | Shop3 | Street3 | | 4 | Shop4 | Street4 | +----+--------------+--------------+
fruits
+----+--------------+--------------+ | id | fruit | price | +----+--------------+--------------+ | 1 | Bannana | 2.5 | | 2 | Apple | 2.1 | | 3 | Orange | 1.8 | | 4 | Plum | 2.2 | +----+--------------+--------------+
availability
+----+--------------+--------------+ | id | shop_id | fruit_id | +----+--------------+--------------+ | 1 | 1 | 2 | | 2 | 2 | 2 | | 3 | 1 | 3 | | 4 | 2 | 1 | +----+--------------+--------------+
shop_activity
+----+--------------+--------------+--------------+ | id | shop_id | user_id | status | +----+--------------+--------------+--------------+ | 1 | 2 | 1 | 1 | | 2 | 3 | 2 | 1 | | 3 | 1 | 2 | 2 | | 4 | 2 | 2 | 1 | +----+--------------+--------------+--------------+
users
+----+--------------+ | id | name | +----+--------------+ | 1 | Peter | | 2 | John | +----+--------------+
I have query
SELECT
availability.shop_id,
shops.name
FROM availability
LEFT JOIN shops
ON availability.shop_id=shops.id
WHERE
fruit_id = 2
As a result I get name list of shops where fruit with id 2 (apple) is available.
What should I do so that I can include shop_activity table in query to get user's status if users
.id
= 1 beside proper shop. Something like this...
Shop1, NULL Shop2, status: 1
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
你可以尝试这样的事情:
You could try something like this:
尝试将商店作为左连接中的第一个表
try making shops as the first table in left join
尝试以下操作:
这应该为每个有苹果的商店提供一行,但对于用户没有活动的商店,状态将显示为空,否则显示该用户的状态。
Try the following:
This should give you a row for every shop with apples, but the status will show as null for shops where the user has no activity, otherwise shows the status of that user.