删除从数据库中显示重复值

时间:2014-02-25 10:31:19

标签: ios sql database parent-child

我有亲子关系问题。当我回答父母问题时,它会显示父母和孩子。当我回答孩子时,它会显示父母和孩子。但如果我同时回答(父母和孩子),则子问题会显示两次。我知道我的查询错了。但无法找到。如何删除从数据库显示的重复数据

2 个答案:

答案 0 :(得分:0)

试试这样....

DELETE
FROM MyTable
WHERE ID NOT IN
(
SELECT MAX(ID)
FROM MyTable
GROUP BY
 DuplicateColumn1, DuplicateColumn2, DuplicateColumn3)

答案 1 :(得分:0)

理解你的查询相当困难。您是否尝试过添加DISTINCT语句?

SELECT DISTINCT * FROM (here paste your query)

因此,您的完整查询将在

之后
SELECT DISTINCT * FROM (
SELECT a.QM_ID,a.QM_QCM_ID,
       a.QM_Question,a.QM_Type,
       a.QM_Parent_Id,
       c.AM_Answer, 
       c.AM_Comments 
  FROM question_master a 
       INNER JOIN Assessment_master c 
             ON (c.AM_QM_ID = a.QM_ID 
                 AND c.AM_HNM_ID = %d 
                 AND c.AM_HM_ID = %d 
                 AND c.AM_ASM_Local_Id = %@) 
 WHERE a.QM_Parent_Id = 0 
       AND a.QM_Status = 'A' 
       AND a.QM_QCM_ID = %@ 
       AND a.QM_QRM_Id = %@

UNION
SELECT b.QM_ID,
       b.QM_QCM_ID,
       b.QM_Question,
       b.QM_Type,
       b.QM_Parent_Id,
       null, 
       null 
  FROM question_master b 
       INNER JOIN Assessment_master d 
             ON (d. AM_QM_ID = b.QM_Parent_Id 
                 AND d.AM_HNM_ID = %d 
                 AND d.AM_HM_ID = %d 
                 AND d.AM_ASM_Local_Id = %@) 
 WHERE b.QM_Parent_Id != 0 
       AND b.QM_Status = 'A' 
       AND b.QM_QCM_ID = %@ 
       AND b.QM_QRM_Id = %@

UNION
SELECT b.QM_ID,
       b.QM_QCM_ID,
       b.QM_Question,
       b.QM_Type,
       b.QM_Parent_Id,
       d.AM_Answer, 
       d.AM_Comments 
  FROM question_master b 
       INNER JOIN Assessment_master d 
             ON (d. AM_QM_ID = b.QM_ID 
                 AND d.AM_HNM_ID = %d 
                 AND d.AM_HM_ID = %d 
                 AND d.AM_ASM_Local_Id = %@) 
 WHERE b.QM_Parent_Id IN (SELECT QM_ID 
                            FROM question_master 
                           WHERE QM_Parent_Id = 0 
                                 AND QM_Status = 'A' 
                                 AND QM_QCM_ID = %@ 
                                 AND QM_QRM_Id = %@)
)