在我的表中,我有一些记录,它们有一些重复的出生日期值,所以我想要那些只有匹配年份和月份的人的id
我得到的输出如下:
mysql - 如何在dob列中获取仅具有匹配的月份和年份的人员的ID-LMLPHP
但我的期望是:

id   dob(column name of table bday)

1  1989-12
3  1989-12
4  1985-11
6  1985-11

MySQL查询1:
SELECT id, dob
FROM bday
WHERE dob IN (
    SELECT dob
    FROM bday
    GROUP BY dob
    HAVING COUNT(id) > 1
)

MySQL查询#2:
SELECT EXTRACT(YEAR_MONTH from dob) FROM bday group by dob having count(*) >1

我得到的输出如下:
id   dob(column name of table bday)
1    1989-12-22
3    1989-12-22
4    1985-11-27
6    1985-11-27

但实际上,我只想要一年一个月的id

最佳答案

    SELECT EXTRACT(YEAR_MONTH from date)
 FROM table group by date having count(*) >= 2

试试这个
select YEAR(date) AS 'year', MONTH(date) AS 'month' COUNT(*) c FROM table GROUP BY column HAVING c > 1

关于mysql - 如何在dob列中获取仅具有匹配的月份和年份的人员的ID,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40218946/

10-17 01:48