从限制和描述的2列获得结果

时间:2014-04-28 11:56:52

标签: mysql sql two-columns

SELECT *
FROM VB_Names
WHERE Buurt = 'Spangen'
    AND Gender  = 'M'
    AND Gender  = 'V'
GROUP BY Name
ORDER BY COUNT DESC LIMIT 6

我想从性别' M'列中获得3个结果。和性别' V'列中的3个结果与Count on DESC一起。

2 个答案:

答案 0 :(得分:5)

性别不能同时包含'M'和'V',因此此查询不会返回任何内容。相反,请使用union all

(SELECT *
 FROM VB_Names
 WHERE Buurt = 'Spangen' AND Gender = 'M'
 ORDER BY count DESC
 LIMIT 3
) UNION ALL
(SELECT *
 FROM VB_Names
 WHERE Buurt = 'Spangen' AND Gender = 'V'
 ORDER BY count DESC
 LIMIT 3
)

答案 1 :(得分:0)

AND条件更改为OR

SELECT *
FROM VB_Names
WHERE Buurt = 'Spangen'
    AND (Gender  = 'M' OR Gender  = 'V')
GROUP BY Name
ORDER BY COUNT DESC LIMIT 6