在MySQL WHERE子句中将GROUP_CONCAT与FIND_IN_SET一起使用

时间:2019-02-14 10:13:46

标签: mysql sql database mysqli

我有此查询,并希望使用学生电子邮件从表中获取约会数据,但它收到此错误Unknown column 'stu' in 'where clause'

select a.*, GROUP_CONCAT(e.value) as stu FROM wp_ea_appointments a join
wp_ea_fields e on a.id = e.app_id WHERE a.date > DATE('2019-02-14') AND
FIND_IN_SET('jan@gmail.com' , stu ) GROUP BY a.id

任何想法都会受到赞赏

3 个答案:

答案 0 :(得分:3)

您可以尝试使用子查询

select * from
(
select a.*, GROUP_CONCAT(e.value) as stu FROM wp_ea_appointments a join
wp_ea_fields e on a.id = e.app_id WHERE a.date > DATE('2019-02-14') 
GROUP BY a.id
)A where FIND_IN_SET('jan@gmail.com' , stu ) 

答案 1 :(得分:2)

FIND_IN_SET条件应该包含在HAVING语句中而不是WHERE语句中,因为您要在分组后过滤数据

SELECT a.*, 
    GROUP_CONCAT(e.value) as stu 
FROM wp_ea_appointments a 
JOIN wp_ea_fields e ON a.id = e.app_id 
WHERE a.date > DATE('2019-02-14')
GROUP BY a.id
HAVING FIND_IN_SET('jan@gmail.com', stu) 

答案 2 :(得分:1)

使用HAVING子句,但正确说明逻辑:

SELECT a.*, GROUP_CONCAT(e.value) as stu
FROM wp_ea_appointments a JOIN
     wp_ea_fields e 
     ON a.id = e.app_id
WHERE a.date > DATE('2019-02-14')
HAVING SUM( e.value = 'jan@gmail.com' ) > 0;

没有理由使用字符串连接来检查值是否存在。字符串比较比较昂贵,而且不清楚。