Mysql左连接空结果
我有这个疑问Mysql左连接空结果,sql,mysql,null,left-join,Sql,Mysql,Null,Left Join,我有这个疑问 SELECT articles.*, users.username AS `user` FROM `articles` LEFT JOIN `users` ON articles.user_id = users.id ORDER BY articles.timestamp 基本上,它返回文章列表和文章关联的用户名。现在,如果用户表中没有特定用户id的条目,usersvar为空。是否有任何方法可以使它在为null时返回类似“User Not Found”
SELECT articles.*,
users.username AS `user`
FROM `articles`
LEFT JOIN `users` ON articles.user_id = users.id
ORDER BY articles.timestamp
基本上,它返回文章列表和文章关联的用户名。现在,如果用户表中没有特定用户id的条目,users
var为空。是否有任何方法可以使它在为null时返回类似“User Not Found”的内容?或者我必须使用php来执行此操作吗?使用:
SELECT a.*,
COALESCE(u.username, 'User Not Found') AS `user`
FROM ARTICLES a
LEFT JOIN USERS u ON u.id = a.user_id
ORDER BY articles.timestamp
文件:
IF()
在Oracle中使用decode
所以
应该有用。
注意:我手头没有mysql,所以没有测试查询。但如果失败,则应进行轻微修改。不要投反对票;) 您可以使用以下功能:
SELECT articles.*, IFNULL(users.username, 'User Not Found') AS `user`
FROM `articles` LEFT JOIN `users` ON articles.user_id = users.id
ORDER BY articles.timestamp
找不到用户的双引号?;)
SELECT articles.*, IFNULL(users.username, 'User Not Found') AS `user`
FROM `articles` LEFT JOIN `users` ON articles.user_id = users.id
ORDER BY articles.timestamp
SELECT articles.*,
IFNULL(users.username,'User Not Found') AS `user`
FROM `articles`
LEFT JOIN `users` ON articles.user_id = users.id
ORDER BY articles.timestamp