簡體   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