我有一个类似于以下的表
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个独立的个人查询来获得上述输出
提前致谢
答案 0 :(得分: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
答案 1 :(得分: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;
答案 2 :(得分:0)
经过多次尝试,我能够获得我想要的数据
查询[&#39;女性&#39;]
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'
查询[&#39;男&#39;]
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'
查询[&#39;男性和女性&#39;]
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'