gpt4 book ai didi

mysql - 如果 SQL 查询没有找到结果,则返回默认值

转载 作者:行者123 更新时间:2023-11-29 12:13:48 25 4
gpt4 key购买 nike

我目前正在使用此查询从列关系和表媒体中选择“profile_picture”,尽管我注意到 html 图像标记内的 return default.png 不适用于结果。有什么办法可以返回:如果错误

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

我当前的查询

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

最佳答案

虽然这可能更容易用您的脚本语言处理,但您可以执行 union all 来获取默认值,因为您是按 id 字段排序的:

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

关于mysql - 如果 SQL 查询没有找到结果,则返回默认值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30202778/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com