MySQL如何选择字段或如果字段为空则设置为空?

时间:2019-06-29 22:13:27

标签: mysql sql

是否可以按照以下方式进行操作?

SELECT (user OR NULL if field IS NULL)
FROM user
WHERE (
    field = 'id'
    OR field IS NULL
)

我的数据库设置是

+-----------
| bookmarks 
+-----------
| id int not null primary key
| title varchar(255) not null
| url varchar(255) not null
| icon varchar(255)
| description varchar(255)
| addedby int foreignkey references users(id) on delete set null
+-----------

+-----------
| users
+-----------
| id int not null primary key
| email varchar(255) not null
| user varchar(255) not null
| pass varchar(255) not null
+-----------

+-----------
| user_bookmarks
+-----------
| user_id foreignkey references user(id) on delete cascade
| bookmark_id foreignkey references bookmarks(id)
| primarykey(user_id, bookmark_id)
+-----------

我要修改的查询是

SELECT DISTINCT B.*, MU.user, MUB.subscribers
FROM user_bookmarks UB, bookmarks B, users U, (
    SELECT bookmark_id, COUNT(bookmark_id) AS subscribers
    FROM user_bookmarks
    GROUP BY bookmark_ID
) AS MUB, (
    SELECT user, id
    FROM users
) AS MU
WHERE MUB.bookmark_id = UB.bookmark_id
AND B.id = MUB.bookmark_id
AND UB.user_id = $id
AND U.id = UB.user_id
AND MU.id = B.addedby

我的查询可能太可怕了,SQL并不是我经常使用的东西。

我想要做的是补偿当用户不再在系统上(通过添加另一个用户的ID查找了书签时,用户(添加了书签(addedby)的用户)仍然订阅(U.id = $id),我想保留用户字段(MU.user),但要填充null

使用$id = 93的示例输出(当前):

id, title, url, icon, description, addedby, user, subscribers
'93', 'title93', 'https://www.somewhere93.com', '', 'Something 93', '93', 'user93', '10'
'94', 'title94', 'https://www.somewhere94.com', '', 'Something 94', '94', 'user94', '10'
'95', 'title95', 'https://www.somewhere95.com', '', 'Something 95', '95', 'user95', '10'
'96', 'title96', 'https://www.somewhere96.com', '', 'Something 96', '96', 'user96', '10'
'97', 'title97', 'https://www.somewhere97.com', '', 'Something 97', '97', 'user97', '10'
'98', 'title98', 'https://www.somewhere98.com', '', 'Something 98', '98', 'user98', '10'
'99', 'title99', 'https://www.somewhere99.com', '', 'Something 99', '99', 'user99', '10'
'100', 'title100', 'https://www.somewhere100.com', '', 'Something 100', '100', 'user100', '10'
'102', 'title102', 'https://www.somewhere102.com', '', 'Something 102', '2', 'user2', '8'

id 101丢失,这是因为(SELECT * FROM bookmarks WHERE id = 101)字段addedby为空,我希望我的查询也返回书签101,与其他仅返回由于用户不再存在,useraddedby为空:

id, title, url, icon, description, addedby
'101', 'title101', 'https://www.somewhere101.com', '', 'Something 101', NULL

3 个答案:

答案 0 :(得分:0)

像这样吗?

SELECT ID, IFNULL(User, 'N/A') User, COALESCE(User, Field, 'N/A') Field
FROM User WHERE (Field = ID) OR (Field IS NULL);

答案 1 :(得分:0)

如果使用Microsoft SQL SERVER,请使用以下语法: SELECT(ISNULL(user,'no user')as ColUser) 来自用户 在哪里(     字段='id' )

答案 2 :(得分:0)

我想您想要一行,如果有可用的话,它具有用户值或null值。如果是这样,则一种方法是union all

select u.user
from user u
where u.field = @id
union all
select null
from dual
where not exists (select 1 from user u where u.field = @id);

但是,更简单的方法(可能是性能更高的方法)是聚合:

select max(u.user) as user
from user u
where u.field = @id;

没有group by的聚合查询总是返回恰好一行。如果没有任何行符合where条件,则值为null -这就是您想要的。

或者,使用子查询的类似方法:

select (select u.user
        from user u
        where u.field = @id
        limit 1
      );