查找重复项,共享一个公共属性

时间:2015-04-22 09:55:38

标签: mysql sql

我正在尝试在Drupal站点上选择重复节点,基本上我需要选择共享一个共同'tnid'(翻译节点ID)的节点,并且还要共享相同的'语言'。

但我无法弄清楚如何编写查询,我想我做了第一部分,找到了常见的tnid节点,如此

SELECT origin.nid, origin.tnid, origin.title, origin.language
FROM node AS origin
JOIN (select nid, tnid from node
      group by tnid having count(tnid) > 1) common_tnid ON common_tnid.tnid = origin.tnid
#JOIN node common_lang ON common_lang.language = origin.language
                      AND common_lang.tnid = origin.tnid
WHERE origin.tnid != 0

考虑到语言部分是我的一大障碍,我将如何将其添加到查询中?因此,我尝试了很多东西。评论。

2 个答案:

答案 0 :(得分:1)

试试这个:

SELECT
  table1.nid nid,
  table1.tnid tnid,
  table1.language language,
  table1.title title
FROM
  (
    SELECT *
    FROM
      table1
    GROUP BY
      tnid, language
    HAVING
      COUNT(*) > 1
  ) dupe
  LEFT JOIN
  table1
    ON dupe.tnid = table1.tnid
       AND dupe.language = table1.language

SQL小提琴:http://sqlfiddle.com/#!9/294cc/1/0

答案 1 :(得分:0)

您可以尝试这样的事情

SELECT origin.id AS origin_id, common.id AS common_id
FROM node AS origin
INNER JOIN node AS common ON common.language = origin.language AND common.tnid = origin.tnid AND origin.id != common.id

我不知道你的表是否有id字段,但是你可以改成两行中不同的字段