如何连接两个表而不重复MySQL中的两列

时间:2017-01-14 14:19:19

标签: mysql database join group-by distinct

我有两张桌子(T1 / t2):

      T1                         T2
   id | value                 id | value
    1 |  10                    1 |   5
    2 |  10                    2 |  15
    3 |   5                    3 |  15
    4 |  15                    4 |   5
    5 |   5                    5 |  10

我根据T1选择T2记录(如果T1具有> =具有相同值的记录数量):

SELECT t.id t2_id, t.value value
FROM t2 as t
WHERE 
(SELECT COUNT(*) FROM t1 WHERE t1.value = t.value) >= (SELECT COUNT(*) FROM t2 WHERE t2.value = t.value)

正如所料,这回归:

   t2_id  |  value
      1   |   5
      4   |   5
      5   |   10

问题是当我尝试不附加t1_id列时 重复任何身份证。订单并不重要。所以我基本上都在寻找 结果如下:

  t1_id  | t2_id  |  value
     3   |   1    |   5
     5   |   4    |   5
     1   |   5    |   10

每次使用JOIN和Group的尝试都以第一次t1_id结束 记录匹配每个价值块。

3 个答案:

答案 0 :(得分:0)

关键字DISTINCT用于消除查询结果中的重复行:

SELECT DISTINCT ... FROM A JOIN B ON ..

答案 1 :(得分:0)

我不确定这是不是最好的方法。

select t2_s1.id t2_id, t2_s0.value
    from t2 t2_s0 
    join (
      select t2_s3.id, @rowno1 := if(@blk1 = value, @rowno1 + 1, 1) as id_i, @blk1 := value
      from (
        select * 
        from t2 order by value, id
      ) t2_s3
      cross join (
        select @rowno1 := 0, @blk1 := null) t2_s2
        order by value
      ) t2_s1 
    on t2_s0.id = t2_s1.id
    join(  
        select value, t1_s3.id, @rowno2 := if(@blk2 = value, @rowno2 + 1, 1) as id_i, @blk2:= value
        from (
          select * from t1 order by value, id) t1_s3
          cross join (
            select @rowno2 := 0, @blk1 := null
          ) t1_s2
        order by value
      ) t1_s1
      on t2_s1.id_i = t1_s1.id_i 
        and t2_s0.value= t1_s1.value
   where 
    (select count(*) from t1 where t1.value= t2_s0.value) >= (select count(*) from t2 where t2.value= t2_s0.value)
  

基本上,我为每个价值块实时生成一个自动增量ID   两个表

╔═══════════════════╗      ╔═══════════════════╗
║        T1         ║      ║        T2         ║
╠════╦═══════╦══════╣      ╠════╦═══════╦══════╣ 
║ id ║ value ║ id_i ║      ║ id ║ value ║ id_i ║
╠════╬═══════╬══════╣      ╠════╬═══════╬══════╣
║  1 ║    10 ║    1 ║      ║  1 ║     5 ║    1 ║
║  2 ║    10 ║    2 ║      ║  2 ║    15 ║    1 ║
║  3 ║     5 ║    1 ║      ║  3 ║    15 ║    2 ║
║  4 ║    15 ║    1 ║      ║  4 ║     5 ║    2 ║
║  5 ║     5 ║    2 ║      ║  5 ║    10 ║    1 ║
╚════╩═══════╩══════╝      ╚════╩═══════╩══════╝
  

生成 id_i 加上列,我可以模拟   一对一关系的独特ids

SQLFiddle

答案 2 :(得分:0)

MariaDB [sandbox]> SELECT T1.ID,T2.ID,T1.VALUE
    -> FROM
    -> (
    -> SELECT * FROM
    -> (
    -> SELECT T2.*,(SELECT COUNT(*) FROM T2 T21 WHERE T21.VALUE = T2.VALUE) T2OBS,
    -> (SELECT COUNT(*) FROM T1 WHERE T1.VALUE = T2.VALUE) T1OBS,
    -> IF(T2.VALUE <> @P,@RN:=1,@RN:=@RN+1) RN,
    -> @P:=T2.VALUE P
    -> FROM (SELECT @RN:=0,@P:=0) RN,T2
    -> ORDER BY T2.VALUE,T2.ID
    -> ) S
    -> WHERE T1OBS >= T2OBS
    -> ) T2
    -> JOIN
    -> (
    -> SELECT T1.*,
    -> IF(T1.VALUE <> @P,@RN:=1,@RN:=@RN+1) RN,
    -> @P:=T1.VALUE P
    -> FROM (SELECT @RN:=0,@P:=0) RN,T1
    -> ORDER BY T1.VALUE,T1.ID
    -> ) T1
    -> ON T1.RN = T2.RN AND T1.VALUE = T2.VALUE
    -> WHERE T1.RN <= T2.T2OBS
    -> ORDER BY T1.VALUE,T1.ID;
+------+------+-------+
| ID   | ID   | VALUE |
+------+------+-------+
|    3 |    1 |     5 |
|    5 |    4 |     5 |
|    1 |    5 |    10 |
+------+------+-------+
3 rows in set (0.00 sec)