[英]sql MySQL error (1241) operand should contain 1 column(s)
first I will like to state that am still a newbie on writing SQL Queries. 首先,我想说明一下,编写SQL查询仍然是一个新手。 I thoroughly searched for an answer on this Error and I got a good number of answers, but none seems to be helpful or I will say I don't really know how to apply the solutions to mine.
我彻底搜索了此错误的答案,并且得到了很多答案,但是似乎都没有帮助,或者我会说我真的不知道如何将解决方案应用于我的解决方案。
Here is my challenge, I have an application table, that stores applicants records with some unique columns eg (dl_number,parent_id,person_id). 这是我的挑战,我有一个申请表,该表存储带有一些唯一列的申请人记录,例如(dl_number,parent_id,person_id)。 The parent_id keeps tracks of individual applicant history records with the his/her first record and each applicant is meant to have a unique dl_number, but for some reasons, some applicants dl_number(s) are not unique, hence a need to identify the records with changing dl_number(s).
parent_id与他/她的第一条记录一起跟踪各个申请人的历史记录,并且每个申请人都具有唯一的dl_number,但是由于某些原因,某些申请人的dl_number不是唯一的,因此需要用更改dl_number。
Below is the SQL Query, that am getting the [sql error (1241) operand should contain 1 column(s)] error on. 下面是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
Please any help on how to solve this, or a better way to solve this. 请提供任何有关如何解决此问题的帮助,或者提供更好的解决方法。
DISTICT is not really a function to be used that way. DISTICT并不是真正使用这种功能。 You can do
SELECT DISTICT column1, column2 FROM table
to get unique rows only, or similarly SELECT column, count(DISTINCT anothercolumn) FROM table GROUP BY column
to get unique rows within a group. 您可以执行
SELECT DISTICT column1, column2 FROM table
仅获得唯一行,或者类似地执行SELECT column, count(DISTINCT anothercolumn) FROM table GROUP BY column
来获得组中的唯一行。
Problem as I understand it: You look for duplicates in your table. 据我了解的问题:您在表中查找重复项。 Duplicates are defined as having identical values of these 3 columns:
dl_number
, parent_id
and birth_date
. 重复定义为这三列的值相同:
dl_number
, parent_id
和birth_date
。
I'm also assuming that id
is a primary key in your table. 我还假设
id
是表中的主键。 If not, replace the t2.id <> t.id
condition with one that uniquely identify your row. 如果不是,请用唯一标识您的行的条件替换
t2.id <> t.id
条件。
If you only wanted to know what are the duplicated groups, this should work: 如果您只想知道重复的组,那么应该可以:
SELECT dl_number, 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_number, parent_id, birth_date
HAVING count(*)>1
If, however, you want to know details of each duplicated row, this query will give you that: 但是,如果您想知道每个重复行的详细信息,此查询将为您提供:
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_number, parent_id, birth_date, id; -- So you have your duplicates nicely next to each other.
Please explain further if I misunderstood your objective, or ask if the solution is not clear enough. 如果我误解了您的目标,请进一步解释,或者询问解决方案是否不够清楚。
**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.**
For example. 例如。
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.