在 LEFT JOIN 查询中包含第三个表

发布于 2024-11-28 07:23:22 字数 2081 浏览 1 评论 0原文

我有五个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

好菇凉咱不稀罕他 2024-12-05 07:23:22

你可以尝试这样的事情:

SELECT
    availability.shop_id,
    shops.name,
     shop_activity.status
FROM availability
LEFT JOIN shops
ON availability.shop_id=shops.id 
LEFT JOIN shop_activity
ON shop_activity.shop_id = availability.shop_id
   and shop_activity.user_id = 1
WHERE
fruit_id = 2

You could try something like this:

SELECT
    availability.shop_id,
    shops.name,
     shop_activity.status
FROM availability
LEFT JOIN shops
ON availability.shop_id=shops.id 
LEFT JOIN shop_activity
ON shop_activity.shop_id = availability.shop_id
   and shop_activity.user_id = 1
WHERE
fruit_id = 2
感性不性感 2024-12-05 07:23:22
SELECT
    availability.shop_id,
    shops.name

FROM shops 
LEFT JOIN availability ON availability.shop_id=shops.id 
LEFT JOIN shop_activity ON shop_activity .shop_id=shops.id
WHERE
fruit_id = 2
and users.id=1

尝试将商店作为左连接中的第一个表

SELECT
    availability.shop_id,
    shops.name

FROM shops 
LEFT JOIN availability ON availability.shop_id=shops.id 
LEFT JOIN shop_activity ON shop_activity .shop_id=shops.id
WHERE
fruit_id = 2
and users.id=1

try making shops as the first table in left join

老旧海报 2024-12-05 07:23:22

尝试以下操作:

SELECT shops.name, shop_activity.status
FROM shops
    INNER JOIN availability ON availability.shop_id = shops.id
        AND availability.fruit_id = 2
    LEFT JOIN shop_activity ON shops.shop_id = shop_activity.shop_id
        AND shop_activity.user_id = 1

这应该为每个有苹果的商店提供一行,但对于用户没有活动的商店,状态将显示为空,否则显示该用户的状态。

Try the following:

SELECT shops.name, shop_activity.status
FROM shops
    INNER JOIN availability ON availability.shop_id = shops.id
        AND availability.fruit_id = 2
    LEFT JOIN shop_activity ON shops.shop_id = shop_activity.shop_id
        AND shop_activity.user_id = 1

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文