gpt4 book ai didi

mysql - 添加 SELECT 查询的最大 id

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

我正在尝试配置为仅选择当前查询的最大 ID,限制为 1。

我有一个到下面的 rextester 的链接,它显示了我想要弄清楚的实际示例。不过,对于其中的错误感到抱歉...不确定我在创建示例时做错了什么。

无论如何,你会看到:

INSERT INTO `profile_img`
VALUES
(1,24,'beach'),
(2,55,'sandals'),
(3,56,'hotel'),
(4,55,'ocean'),
(5,55,'corona'),
(6,55,'tacos')
;

我的profile_img中有哪些值 table 。现在,我的查询选择了此 ON p.user_id = f.friend_one 的所有个人资料图像。 。导致重复输出。它看起来像这样:

enter image description here

我只是想让它输出

55    
56

以及相应的最大 ID img用它。谁知道我怎么只能SELECT img 的最大 ID profile_img栏目与friend_one匹配的user_id的表? ON p.user_id = f.friend_one .

但是,有时用户没有 profile_img。以前我在查询中使用过它来为那些没有图像的人获取默认图像。 (case when p.img <> '' then p.img else 'profile_images/default.jpg' end) as img

SELECT f.*
, p.*
FROM friends f
LEFT JOIN profile_img p
ON p.user_id = f.friend_one
WHERE f.friend_two = ?
AND f.status = ?

http://rextester.com/RLXS27142

最佳答案

如果我理解正确的话,您只需要具有最大 id 的个人资料图片:

SELECT f.*, p.*,
COALESCE(p.img, 'profile_images/default.jpg') as profile_img
FROM friends f LEFT JOIN
profile_img p
ON p.user_id = f.friend_one AND
p.id = (select max(p2.id) from profile_img p2 where p2.user_id = p.user_id)
WHERE f.friend_two = ? AND f.status = ?;

关于mysql - 添加 SELECT 查询的最大 id,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40777170/

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