gpt4 book ai didi

使用别名的mysql查询抛出错误

转载 作者:太空宇宙 更新时间:2023-11-03 11:57:00 24 4
gpt4 key购买 nike

运行以下查询

SELECT p.id as pid,p.name as pname, p.email,p.phone,p.created as pcreated,
p.updated as pupdated, GROUP_CONCAT(g.name) AS groups FROM ft_smsender_persons p
JOIN ft_smsender_persons2groups AS p2g ON p.id = p2g.person_id JOIN ft_smsender_groups AS g ON g.id = p2g.group_id
WHERE 1=1 AND deleted = 0 AND p.id as pid LIKE '%7%' OR pname LIKE '%7%' OR
email LIKE '%7%' OR phone LIKE '%7%' OR pcreated LIKE '%7%'
OR pupdated LIKE '%7%' OR groups LIKE '%7%' group by pname, email ORDER BY pid asc

p.id as pid LIKE '%7%' 上给我一个错误

如果我像 pid like %7% 那样使用它,它会给我一个错误,提示 pid is unknown column

查询是动态生成的,所以我的范围很小,如何通过我的代码修复它; mysql有什么办法吗?

最佳答案

可以在查询选择列表中使用别名来为列赋予不同的名称。您可以在 GROUP BY、ORDER BY 或 HAVING 中使用别名引用列的子句。

但是,

标准 SQL 不允许引用列
WHERE 子句中的别名。这个限制是强加是因为当 WHERE 子句是评估,列值可能还没有已确定。

详情:https://dev.mysql.com/doc/refman/5.0/en/problems-with-alias.html

关于使用别名的mysql查询抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31972692/

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