繁体   English   中英

sql MySQL错误(1241)操作数应包含1列

[英]sql MySQL error (1241) operand should contain 1 column(s)

首先,我想说明一下,编写SQL查询仍然是一个新手。 我彻底搜索了此错误的答案,并且得到了很多答案,但是似乎都没有帮助,或者我会说我真的不知道如何将解决方案应用于我的解决方案。

这是我的挑战,我有一个申请表,该表存储带有一些唯一列的申请人记录,例如(dl_number,parent_id,person_id)。 parent_id与他/她的第一条记录一起跟踪各个申请人的历史记录,并且每个申请人都具有唯一的dl_number,但是由于某些原因,某些申请人的dl_number不是唯一的,因此需要用更改dl_number。

下面是SQL查询,正在获取[sql错误(1241)操作数应包含1列]错误。

SELECT id,application_id,dl_number,surname,firstname,othername,birth_date,status_id,expiry_date,person_id,COUNT(DISTINCT(dl_number,parent_id,birth_date)) AS NumOccurrences
FROM tbl_dl_application
WHERE status_id > 1
GROUP BY dl_number,parent_id,birth_date
HAVING NumOccurrences > 1

请提供任何有关如何解决此问题的帮助,或者提供更好的解决方法。

样品表和预期结果

DISTICT并不是真正使用这种功能。 您可以执行SELECT DISTICT column1, column2 FROM table仅获得唯一行,或者类似地执行SELECT column, count(DISTINCT anothercolumn) FROM table GROUP BY column来获得组中的唯一行。

据我了解的问题:您在表中查找重复项。 重复定义为这三列的值相同: dl_n‌​umberparent_idbirth‌​_date

我还假设id是表中的主键。 如果不是,请用唯一标识您的行的条件替换t2.id <> t.id条件。

如果您只想知道重复的组,那么应该可以:

SELECT dl_n‌​umber, parent_id, birth‌​_date, count(*) as NumOccurences  -- You can only add aggregation functions here, not another column unless you group by it.
FROM tbl_dl_application t
WHERE status_id > 1 -- I don't know what this is but it should do no harm.
GROUP BY dl_n‌​umber, parent_id, birth‌​_date
HAVING count(*)>1

但是,如果您想知道每个重复行的详细信息,此查询将为您提供:

SELECT *
FROM tbl_dl_application t
WHERE 
    status_id > 1 -- I don't know what this is but it should do no harm.
    AND EXISTS (
        SELECT 1 
        FROM tbl_dl_application t2
        WHERE
            t2.dl_number = t.dl_number
            AND t2.parent_id = t.parent_id
            AND t2.birth_date = t.birth_date
            AND t2.id <> t.id
    )
ORDER BY dl_n‌​umber, parent_id, birth‌​_date, id;  -- So you have your duplicates nicely next to each other.

如果我误解了您的目标,请进一步解释,或者询问解决方案是否不够清楚。

**You have to use only one column while use to DISTINCT function. You used this three field dl_number,parent_id,birth_date. Just use 1 filed from these 3. Then query will run.**

例如。

SELECT id,application_id,dl_number,surname,firstname,othername,birth_date,status_id,expiry_date,person_id,COUNT(DISTINCT(parent_id)) AS NumOccurrences
FROM tbl_dl_application
WHERE status_id > 1
GROUP BY dl_number,parent_id,birth_date
HAVING NumOccurrences > 1

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM