3

I have this query and want get appointment data from the table using student email but it get this error 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
Dharman
  • 30,962
  • 25
  • 85
  • 135
Khalil DaNish
  • 547
  • 5
  • 18

3 Answers3

5

FIND_IN_SET condition should be contained in HAVING statement instead of WHERE statement because you want to filter data after grouping

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) 
Maksym Fedorov
  • 6,383
  • 2
  • 11
  • 31
3

You can try using subquery

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 ) 
Fahmi
  • 37,315
  • 5
  • 22
  • 31
1

Use a HAVING clause, but phrase the logic correctly:

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;

There is no reason to use string concatenation to check whether the value exists. The string comparisons are more expensive and less clear.

Gordon Linoff
  • 1,242,037
  • 58
  • 646
  • 786