2011-11-16 180 views
1

我不确定这是否可以在没有嵌套选择的单个select语句中执行。Mysql Group BY with if语句

我将我的结果分组,并且我想知道如果整个分组内的字段包含条件,则显示yes。有了这个就只取第一行从组和检查车况而不是组

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

你能发表你正在使用的查询吗? –

+0

想出来: if(GROUP_CONCAT(field)LIKE'%condition%','yes','no') – zbestzeus

+0

我很高兴你想通了。请将您的解决方案作为回答并标记为已接受。这将从未解答的列表中删除您的问题。谢谢! –

回答

1

例如表中的所有行:ID,得分

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 

运作的?

+0

这应该工作,但我试图不嵌套选择作为查询条件有4个内部连接已经...因此,每个嵌套选择将有4个内部连接,我有6个条件我正在检查哪些是6个条件,每个内部加入4个BLAH! – zbestzeus

0
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 
0

既然你已经做一组,你应该能够添加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”会将所有符合条件的列表浮动到列表顶部......然后,您可以通过其他方式(例如姓氏,名字或其他您所查询的字段)进行子命令。