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
  • 26,923
  • 21
  • 73
  • 125
Khalil DaNish
  • 517
  • 4
  • 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,413
  • 2
  • 9
  • 30
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
  • 36,607
  • 5
  • 19
  • 28
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,198,228
  • 53
  • 572
  • 709