【问题标题】:Mysql Group BY with if statementMysql Group BY with if 语句
【发布时间】:2011-11-16 22:15:01
【问题描述】:

我不确定这是否可以在没有嵌套选择的情况下在单个选择语句中执行。

我正在对我的结果进行分组,我想知道如果整个分组中的某个字段包含一个条件,则显示是。有了这个,它只会从组中取出第一行并检查条件而不是组中的所有行

if(field = 'condition','yes','no') as field_found

【问题讨论】:

  • 您可以发布您正在使用的查询吗?
  • 想通了:if(GROUP_CONCAT(field) LIKE '%condition%','yes','no')
  • 我很高兴你知道了。请将您的解决方案作为答案发布并标记为已接受。这会将您的问题从未回答列表中删除。谢谢!
  • 没有足够高的代表,哈哈

标签: mysql if-statement group-by


【解决方案1】:

示例表:id、score

SELECT t1.id, (
  SELECT IF("10" IN (
    SELECT score FROM table t2 WHERE t1.id = t2.id
  ),'yes','no')) 
FROM table t1
GROUP BY t1.id

这行得通吗?

【讨论】:

  • 这应该可以,但我试图不嵌套选择,因为查询以查找条件已经有 4 个内连接...所以每个嵌套选择都有 4 个内连接,我有 6 个条件'正在检查哪个是 6 个条件,每个条件有 4 个内部连接和 BLAH!
【解决方案2】:

由于您已经在进行分组,因此您应该能够将 MAX() 添加为具有您期望的条件的列,然后将其添加到组中...例如

select 
      MAX( if( field LIKE '%condition%','1', '2' )) as ExtraOrderBy,
      First_Name,
      Last_Name,
      ... rest of query ...
   group by
      customers.Customer_ID
   order by
      1

在这种情况下,order by 是 SQL 列表中的序号列,而不是显式重新键入 MAX( IF() ) 条件...因此,如果条件为真,则将其标记为“1”,否则为“ 2" 将浮动所有符合列表顶部的条件...然后,您可以按姓氏、名字或您查询的其他字段等其他内容进行子排序。

【讨论】:

    【解决方案3】:
    if(GROUP_CONCAT(field) LIKE '%condition%','yes','no')
    
    SELECT first_name,last_name, CONCAT(physical_address," ",physical_address2," ",city, " ",zip) as address, MONTHNAME(install_date) as billing_month, IFNULL(status.quantity,0) as total_measures_instalclient2d,IFNULL(client1_measures,"") as client1_measures,IFNULL(client2_measures,"") as client2_measures,IFNULL(client1_quantity,0) as client1_quantity,IFNULL(client2_quantity,0) as client2_quantity,if(GROUP_CONCAT(measure_list.measure_type) LIKE '%Outreach/ Assessment%','yes','no') as outreach_invoiced,GROUP_CONCAT(IF(client='Client1',CONCAT(percent*100,"%-",measure_list.measure_type),NULL)) as client1_percent,GROUP_CONCAT(IF(client='Client2',CONCAT(percent*100,"%-",measure_list.measure_type),NULL)) as client2_percent,work_order.notes FROM customers
    INNER JOIN measure on measure.customer_id = customers.customer_id
    INNER JOIN measure_list on measure_list.measure_list_id = measure.measure_list_id
    INNER JOIN work_order on work_order.work_order_id = measure.work_order_id
    INNER JOIN billing on billing.workmanship = work_order.workmanship AND billing.measure_type = measure_list.measure_type
    LEFT JOIN (
    SELECT customers.customer_id,SUM(quantity) as quantity,GROUP_CONCAT(IF(client='Client1',measure_description,NULL)) as client1_measures,GROUP_CONCAT(IF(client='client2',measure_description,NULL)) as client2_measures,SUM(IF(client='client1',quantity,0)) as client1_quantity,SUM(IF(client='client2',quantity,0)) as client2_quantity FROM customers
    INNER JOIN measure on measure.customer_id = customers.customer_id
    INNER JOIN measure_list on measure_list.measure_list_id = measure.measure_list_id
    INNER JOIN work_order on work_order.work_order_id = measure.work_order_id
    INNER JOIN billing on billing.workmanship = work_order.workmanship AND billing.measure_type = measure_list.measure_type
    WHERE measure_list.measure_type NOT IN ('measure1','measure2')
    GROUP BY customers.customer_id
    ) as status on status.customer_id = customers.customer_id
    WHERE measure_list.measure_type IN ('measure1','measure2')
    GROUP BY customers.customer_id
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-29
      • 2020-10-26
      • 2020-04-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-15
      • 2011-10-23
      • 1970-01-01
      相关资源
      最近更新 更多