简体   繁体   English

Mysql:仅当列为空时才加入

[英]Mysql: Join only when column is empty

I am trying to write a query in MySQL to fetch a list of images. 我试图在MySQL中编写一个查询来获取图像列表。 The problem is that, I want to join another table only when a column is not null. 问题是,我想只在列不为null时才加入另一个表。

images: 图片:

|id|image                                                               |user_id|circle_id|is_deleted|
------------------------------------------------------------------------------------------------------
|1 |cdb10bee27262c597619fdef25bae0eb673f5d5d1b9b33697e40906ad851a25b.jpg|30     |1        |0         |
|2 |9e3213276225b900a3e19262a7761ec3fdf608b95441aa8fbc012f246514d394.jpg|30     |1        |0         |
|3 |a51e104c82de33a804ae0a09e719f3cd7aa205a0830eab4e1e4aa26fd8a6313d.jpg|30     |NULL     |0         |

circles_users: circles_users:

|id|circle_id|user_id|
----------------------
|1 |1        |31     |

users: 用户:

|id|username|
-------------
|30|elk     |
|31|moose   |

Initially, I came up with this, but after inspection, I realised it would not work. 最初,我想出了这个,但经过检查,我意识到它不会起作用。

SELECT images.*
FROM images
    LEFT JOIN circles_users ON circles_users.circle_id = images.circle_id
WHERE (circles_users.user_id = '31' OR images.circle_id IS NULL)
    AND images.user_id = '30'
    AND images.is_deleted = '0'
LIMIT 16

I can't seem to get it right anyhow, so I decided to write some pseudo sql to illustrate what I wish to achieve 无论如何我似乎无法做到正确,所以我决定编写一些 sql来说明我希望实现的目标

SELECT images.*
    FROM images

        CASE
            WHEN images.circle_id IS NOT NULL 
            THEN LEFT JOIN circles_users ON circles_users.circle_id = images.circle_id AND circles_users.user_id = '32'
        END CASE

    WHERE images.user_id = '30'
    AND images.is_deleted = '0'
    LIMIT 16

Hope you get what I mean. 希望你明白我的意思。 Thanks for the help. 谢谢您的帮助。

Basically, I want to fetch all images posted by a user (poster) has if another user is in the circle shared by the poster or if the circle_id is null 基本上,我想获取用户发布的所有图像(海报),如果另一个用户在海报共享的圆圈中,或者如果circle_id为null

如果我理解你的问题,你应该使用INNER JOIN

Then Change your query to something like 然后将您的查询更改为类似的内容

SELECT im.*
FROM images im
    LEFT JOIN circles_users cu ON cu.circle_id = im.circle_id AND cu.user_id = '31'
WHERE im.user_id = '30'
    AND im.is_deleted = '0'
LIMIT 16

My test data was flawed and my trust in my sql knowledge ahaky. 我的测试数据有缺陷,我对我的sql知识的信任很差。 The original query is correct and works as expected. 原始查询是正确的,并按预期工作。

SELECT images.* FROM images LEFT JOIN circles_users ON circles_users.circle_id = images.circle_id WHERE (circles_users.user_id = '31' OR images.circle_id IS NULL) AND images.user_id = '30' AND images.is_deleted = '0' LIMIT 16 选择图像。* FROM图像LEFT JOIN circles_users ON circles_users.circle_id = images.circle_id WHERE(circles_users.user_id = '31'或images.circle_id IS NULL)AND images.user_id ='30'AND images.is_deleted ='0'LIMIT 16

EXPLANATION When the query is run, it fetches all images with user_id = 30. If circle_id is not null, it fetches the corresponding circles_users row else nothing as it is a left join The Where statement then checks if the circles_users.user_id (if it exists) = 31 else it checks if images.circle_id is null effectively achieving the What I aimed to. 说明运行查询时,它将使用user_id = 30获取所有图像。如果circle_id不为null,则取出相应的circles_users行,因为它是左连接,然后Where语句检查circles_users.user_id(如果存在) )= 31否则它检查images.circle_id是否为null有效地实现了我的目的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM