2016-08-22 178 views
2

多列我有類似以下MySQL的 - 組由同一個表

sno | booking_id | room_type | gender | age | amount | days 
1 | 2016JUL001 | AC  | Male | 25 | 1000 | 15 
2 | 2016JUL001 | AC  | Male | 42 | 1000 | 15 
3 | 2016JUL001 | AC  | Male | 28 | 1000 | 15 
4 | 2016JUL010 | N AC  | Female | 45 | 1000 | 15 
5 | 2016JUL010 | N AC  | Female | 46 | 1000 | 15 
6 | 2016JUL005 | N AC  | Male | 28 | 1000 | 15 
7 | 2016JUL005 | N AC  | Female | 35 | 1000 | 15 
8 | 2016JUL009 | AC  | Female | 26 | 1000 | 15 
9 | 2016JUL009 | AC  | Female | 25 | 1000 | 15 

表...等等

預期輸出[如果我想獲得性別=「女」]

sno | booking_id | room_type | gender | age | amount | days 
4 | 2016JUL010 | N AC  | Female | 45 | 1000 | 15 
5 | 2016JUL010 | N AC  | Female | 46 | 1000 | 15 
8 | 2016JUL009 | AC  | Female | 26 | 1000 | 15 
9 | 2016JUL009 | AC  | Female | 25 | 1000 | 15 

預期輸出[如果我想獲得性別= '男']

sno | booking_id | room_type | gender | age | amount | days 
1 | 2016JUL001 | AC  | Male | 25 | 1000 | 15 
2 | 2016JUL001 | AC  | Male | 42 | 1000 | 15 
3 | 2016JUL001 | AC  | Male | 28 | 1000 | 15 

預期輸出[如果我想獲得性別=「男」,性別=「女」]

sno | booking_id | room_type | gender | age | amount | days 
6 | 2016JUL005 | N AC  | Male | 28 | 1000 | 15 
7 | 2016JUL005 | N AC  | Female | 35 | 1000 | 15 

注:我想3個分開的單個查詢,以得到上面的輸出

在由於推進

+1

問題是什麼? – sagi

+0

請閱讀完整的文章,我已經提到了3個預期的輸出和查詢要修改@sagi –

+0

sathvik能否請你擴展你的問題 –

回答

0

很多的嘗試之後,我能夠得到我想要

查詢數據[「女」]

SELECT sno, bd.booking_id, bd.room_type, bd.gender, bd.age 
FROM customer_data bd 
INNER JOIN ( 
    SELECT booking_id, GROUP_CONCAT(DISTINCT gender) AS g 
    FROM customer_data 
    WHERE gender!='' AND age>0 
    GROUP BY booking_id 
    HAVING COUNT(booking_id) > 1 
    ORDER BY booking_id ASC, gender DESC 
) cbd 
WHERE cbd.booking_id = bd.booking_id AND cbd.g = 'Female' 

查詢[ '男']

SELECT sno, bd.booking_id, bd.room_type, bd.gender, bd.age 
FROM customer_data bd 
INNER JOIN ( 
    SELECT booking_id, GROUP_CONCAT(DISTINCT gender) AS g 
    FROM customer_data 
    WHERE gender!='' AND age>0 
    GROUP BY booking_id 
    HAVING COUNT(booking_id) > 1 
    ORDER BY booking_id ASC, gender DESC 
) cbd 
WHERE cbd.booking_id = bd.booking_id AND cbd.g ='Male' 

查詢[ '男性和女性']

SELECT sno, bd.booking_id, bd.room_type, bd.gender, bd.age 
FROM customer_data bd 
INNER JOIN ( 
    SELECT booking_id, GROUP_CONCAT(DISTINCT gender ORDER BY gender DESC) AS g 
    FROM customer_data 
    WHERE gender!='' AND age>0 
    GROUP BY booking_id 
    HAVING COUNT(booking_id) > 1 
    ORDER BY booking_id ASC, gender DESC 
) cbd 
WHERE cbd.booking_id = bd.booking_id AND cbd.g = 'Male,Female' 
+1

看起來效率很低。 A)在尋找完美匹配時,用'='替換'LIKE'。 B)你可以通過'GROUP_CONCAT()'來訂購,因此它將始終是相同的訂單,你不必同時看到'男性,女性'和'女性,男性'。看看我的句子,你可以將它添加到內部查詢中,添加到having子句'HAVING MAX(gender)='female'' for female female,'HAVING MIN(gender)='man'' for men。和'具有計數(DISTINCT GENDER)= 2'。 – sagi

+0

是的,我已經在我的代碼@sagi中進行了優化。謝謝,我已經回答了,因爲其他人會從中得到一些想法。無論如何,我也會在這裏編輯。 –

+0

我認爲'HAVING MAX(gender)='female''這在某些情況下不起作用 –

3

首先查詢:

SELECT sno, booking_id, room_type, gender, age 
FROM customer_data 
WHERE booking_id IN (SELECT booking_id FROM customer_data 
         WHERE gender='female' AND age>0 and RIGHT(booking_id,1) <> '1' 
         GROUP BY booking_id HAVING COUNT(*) > 1) 
ORDER BY booking_id ASC, age ASC 

二:

SELECT sno, booking_id, room_type, gender, age 
FROM customer_data 
WHERE booking_id IN (SELECT booking_id FROM customer_data 
         WHERE gender='male' AND age>0 
         GROUP BY booking_id HAVING COUNT(*) > 1) 
ORDER BY booking_id ASC, age ASC 

;第三:

SELECT sno, booking_id, room_type, gender, age 
FROM customer_data 
WHERE booking_id IN (SELECT booking_id FROM customer_data 
         WHERE gender IN('male','female') AND age>0 
         GROUP BY booking_id HAVING COUNT(distinct gender) = 2) 
ORDER BY booking_id ASC, age ASC 

如果前兩個你想僅僅只有1個性別booking_id,加having子句:

AND COUNT(distinct gender) = 1 
+0

預訂ID與我提到的不一樣..它會改變 –

+0

會改變如何?怎麼了?你是否期待我猜測你需要什麼? – sagi

+0

謝謝。 @Manishsharma – sagi

0

你的模式似乎有點瑕疵。然而,這裏的一些思考......

SELECT booking_id 
    , COUNT(DISTINCT gender) x 
    FROM customer_data 
WHERE gender IN ('Male','Female') <-- not strictly necessary if there are only two genders. 
GROUP 
    BY booking_id; 
+0

通過他的查詢,我假設還有一個空字符串值的性別。 – sagi

+0

@sagi是的,我認爲你是對的 - 所以這是必要的 – Strawberry