简体   繁体   中英

How to apply searching on aggregate function with normal column in mysql

I have a two table one is survey_list and another is survey_summary in which in which i have to make a query which can sort all the column from both the tables but in which i am using one count function also so that i was unable to find out how can i sort the value which is coming from count function with the normal column.

Below is my table structure.

first table is survey_list

..........................
id   surveyname   status
..........................
1      test1      Active
2      test2      Inactive

second table survey_summary

................................
id    userid       survey_id
................................
1      46              1
2      47              2
3      48              1
4      49              2

in the survey_summary table survey_id is the fk of survey_list which has a id.I have tried below query to search all the column but i was unable to apply searching on count column.

SELECT survey_list.`id`,
       survey_list.`survey_name`,
       survey_list.`status`,
       count(survey_list.`id`)AS COUNT
FROM `survey_list`
LEFT JOIN survey_summary ON survey_list.`id`=survey_summary.`survey_id`
WHERE survey_list.`survey_name` LIKE '2%'
  OR survey_list.`status` LIKE '2%'
GROUP BY survey_list.`id`

Please help me to apply searching on the count column as well.

Thanks in Advance

Is it that you are looking for?

   SELECT survey_list.`id`,
           survey_list.`survey_name`,
           survey_list.`status`,
           count(survey_list.`id`)AS cnt
    FROM `survey_list`
    LEFT JOIN survey_summary ON survey_list.`id`=survey_summary.`survey_id`
    WHERE survey_list.`survey_name` LIKE '2%'
      OR survey_list.`status` LIKE '2%'
    GROUP BY survey_list.`id`
    HAVING
     cnt > 0

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM