gpt4 book ai didi

mysql - 为什么我不能使用这个参数?

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

我试图在 UNION 的两个选择中使用“id”,有什么方法可以做到吗?

SELECT id, name, (

SELECT DATE_FORMAT(a.time_added, '%d/%m/%Y') AS 'last_update' FROM analysis_groups ag, analysis_group_relation agr, analysis a WHERE ag.id = id AND agr.group_id = ag.id AND a.id = agr.analysis_id AND a.type != 3
UNION
SELECT DATE_FORMAT(a.time_added, '%d/%m/%Y') AS 'last_update' FROM analysis_groups ag, analysis_group_relation agr, analysis a, user_analysis ua WHERE agr.group_id = ag.id AND a.id = agr.analysis_id AND a.type = 3 AND ua.analysis_id = a.id AND a.id = '5bf1d6c5242f1' ORDER BY last_update LIMIT 1
) AS last_update_date

FROM analysis_groups WHERE id != '0' ORDER BY last_update_date DESC LIMIT 20;

最佳答案

简短的答案是:在主查询中为 analysis_groups 定义一个别名,并在 WHERE 子句中引用它,如下所示

SELECT id, name, (

SELECT DATE_FORMAT(a.time_added, '%d/%m/%Y') AS 'last_update' FROM analysis_groups ag, analysis_group_relation agr, analysis a WHERE ag.id = ag2.id AND agr.group_id = ag.id AND a.id = agr.analysis_id AND a.type != 3
UNION
SELECT DATE_FORMAT(a.time_added, '%d/%m/%Y') AS 'last_update' FROM analysis_groups ag, analysis_group_relation agr, analysis a, user_analysis ua WHERE agr.group_id = ag.id AND a.id = agr.analysis_id AND a.type = 3 AND ua.analysis_id = a.id AND a.id = '5bf1d6c5242f1' ORDER BY last_update LIMIT 1
) AS last_update_date

FROM analysis_groups ag2 WHERE id != '0' ORDER BY last_update_date DESC LIMIT 20;

关于mysql - 为什么我不能使用这个参数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53439188/

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