简体   繁体   English

如果在SQL查询中找不到结果,则返回默认值

[英]Return default value if no results found in SQL query

I currently am using this query to select "profile_picture from the column relation and table media, although I noticed my return default.png inside my html image tag doesn't work for results. Is there any way I could return: IF error 我目前正在使用此查询从列关系和表媒体中选择“ profile_picture”,尽管我注意到我的html image标签中的return default.png不适用于结果。有什么方法可以返回:IF错误

"SELECT * FROM media WHERE post_id = '5552773e65a2b' AND relation = 'profile_picture"

My current query 我目前的查询

"SELECT * FROM media WHERE userID = $friend->user_id AND relation = 'profile_picture' ORDER BY id DESC LIMIT 1"

While this would probably be easier to handle with your scripting language, you could do a union all to get the default value since you're ordering by the id field: 虽然使用脚本语言可能会更容易处理,但是由于要通过id字段进行排序,因此您可以进行并union all以获取默认值:

SELECT id, picture 
FROM media 
WHERE userID = $friend->user_id 
    AND relation = 'profile_picture' 
UNION ALL 
SELECT -1 id, 'default.png' picture
ORDER BY id DESC LIMIT 1

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

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