2011-11-16 176 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」會將所有符合條件的列表浮動到列表頂部......然後,您可以通過其他方式(例如姓氏,名字或其他您所查詢的字段)進行子命令。