简体   繁体   English

MySQL-如果字段具有值,则选择行,否则选择空值

[英]MySQL - Select row if field has value, otherwise select with null value

I'm trying to create query with user_id parameter that would select one row per article_id where row with set user_id has priority. 我正在尝试使用user_id参数创建查询,该查询将为每个article_id选择一行,其中设置了user_id的行具有优先级。 For example: 例如:

If user_id = 1, rows with id 2, 3, 4 should be selected. 如果user_id = 1,则应选择ID为2、3、4的行。

If user_id = 2, rows with id 1, 3, 5 should be selected. 如果user_id = 2,则应选择ID为1、3、5的行。

If user_id = 17, rows with id 1, 3, 4 should be selected. 如果user_id = 17,则应选择ID为1、3、4的行。

Please consider pair (user_id, article_id) unique. 请考虑一对(user_id,article_id)唯一。

id   user_id   article_id
1    null      8
2    1         8
3    null      9
4    null      10
5    2         10

You can use this query: 您可以使用以下查询:

SELECT *
FROM articles a1
WHERE user_id = 1 OR
    user_id IS NULL AND NOT EXISTS(SELECT * FROM articles a2 WHERE a2.article_id = a1.article_id AND a2.user_id = 1)
ORDER BY user_id IS NULL

This will find all the articles which match user_id = 1, or a NULL user_id where there was not a matching entry with the requested user_id . 这将找到所有与user_id = 1匹配的文章,或者在没有与所请求的user_id匹配的条目的情况下为NULL user_id的文章。 The results are ordered by user_id IS NULL which will be 0 for an article which matches the user_id , and 1 for an article where user_id is NULL, thus prioritising the actual matches on user_id . 结果按user_id IS NULL排序,对于与user_id匹配的文章,该值为0,对于user_id为NULL的文章,其值为1,因此优先考虑与user_id的实际匹配。

Output (for user_id=1 ): 输出(对于user_id=1 ):

id  user_id     article_id
2   1           8
3   (null)      9
4   (null)      10

Output (for user_id=2 ): 输出(对于user_id=2 ):

id  user_id     article_id
5   2           10
1   (null)      8
3   (null)      9

Output (for user_id=17 ): 输出(对于user_id=17 ):

id  user_id     article_id
1   (null)      8
3   (null)      9
4   (null)      10

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

相关问题 使用MySQL查询时,如果字段具有特定值,则将其包含在SELECT查询中,否则,请勿包含 - With a MySQL Query, If a Field has a particular value, then include it in the SELECT query otherwise, don't include it MySQL通过在数组字段中搜索来选择行,如果找到值,则选择该行 - MySQL select row by searching in array field, and if value found, select that row MySQL:其中(如果值存在,则非0-选择值;否则,选择0) - MySQL: where (if value exists other then 0 - select value; otherwise, select 0) mysql:条件为条件的列为NULL值时选择查询 - mysql: select query when the column that has condition on has NULL value 根据邻居行中字段的值在mysql中选择一行 - Select a row in mysql depending on a value of a field in neighbor row MYSQL根据值(如果存在)对SELECT行进行查询,否则从另一个表返回数据 - MYSQL Query to SELECT row based on a value (if exists) otherwise return data from another table MySQL - 如果为空,则选择默认值 - MySQL - SELECT default value if null Mysql如何选择字段数组包含此值的行 - Mysql how to select row where field's array contains this value MySQL:仅在最接近日期具有列值的地方选择行 - MySQL: select row only where closest to date has column value 如何在 MySQL 中字段具有最小值的 select 数据? - How to select data where a field has a min value in MySQL?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM