简体   繁体   English

如何在mysql中使用普通列对聚合函数应用搜索

[英]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. 我有两个表,一个是Survey_list,另一个是Survey_summary,在其中我必须进行查询,该查询可以对两个表中的所有列进行排序,但是其中我也在使用一个count函数,因此我无法找出我如何对来自正常列的计数功能的值进行排序。

Below is my table structure. 下面是我的表结构。

first table is survey_list 第一个表是survey_list

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

second table survey_summary 第二张表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. 在survey_summary表中survey_id是具有ID的Survey_list的fk。我曾尝试在下面的查询中搜索所有列,但无法在count列上应用搜索。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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