MYSQL Advanced选择具有相应ID的不同值,这些值不在其他表中

时间:2015-11-01 08:17:44

标签: mysql distinct

编辑:

我的某个表中的SELECT DISTINCT值存在很大问题。

表1:T1

pid      thing       sub-thing     tnumber     
-------------------------------------------
1       A1212       A01A00001     123456      
2       A1212       A01A00002     123457     
3       A1212       A01A00002     123458      
4       A1214       A01B00001     123459     
5       A1214       A01B00002     123460        
6       A1214       A01B00001     123461         
7       A1217       C01A00001     123462     

表2:T2

id        pid      thing       sub-thing         tnumber    h1 
-------------------------------------------------------------- 
1          3            A1212       A01A00002     123458   False

对于表SELECT中不在表DISTINCT中的所有内容,我需要T1所有T2子事项,最重要的是 - 只有子事项最高的数字(例如,有max(id))。

结果应该从T1中留下记录2,4,因为有相同的子事件具有更高的数字,当然从T1留下记录3,因为它在T2中。 最终结果应该是这样的

id      thing       sub-thing     tnumber     
-------------------------------------------
1       A1212       A01A00001     123456                 
5       A1214       A01B00002     123460        
6       A1214       A01B00001     123461         
7       A1217       C01A00001     123462  

感谢大家,我尝试的语法是:

SELECT DISTINCT t1.pid, t1.thing, t1.subthing, t1.tnumber 
FROM t1 INNER JOIN
(SELECT t1.thing, t1.subthing, max(t1.tnumber) as tnumber FROM t1 LEFT OUTER JOIN t2 o ON t1.pid=o.pid WHERE o.pid IS NULL
AND t1.added>'2015-10-31' 
GROUP BY t1.subthing, thing) 
as b using (subthing, thing, tnumber) ;

现在我需要添加一个条件,T1 T1.pid不在T2.pid中的记录和t2.h1=false中的记录不应在最终结果中给出。

5 个答案:

答案 0 :(得分:2)

此查询将返回每个子元素的MAX(id):

SELECT subthing, MAX(id) AS max_id
FROM t1
WHERE subthing NOT IN (SELECT subthing FROM t2) 
GROUP BY subthing;

(我想你想要排除t2中存在的所有子东西)

然后使用此查询,您可以检索最大ID的所有其他列:

SELECT t1.*
FROM t1
WHERE ID IN (
    SELECT MAX(id) AS max_id
    FROM t1
    WHERE subthing NOT IN (SELECT subthing FROM t2) 
    GROUP BY subthing
  )

请看小提琴here。如果你想要更好的表现,你可以尝试LEFT JOIN:

SELECT ta.*
FROM
  t1 AS ta LEFT JOIN t1 AS tb
  ON ta.subthing=tb.subthing
     AND ta.id<tb.id
WHERE
  ta.subthing NOT IN (SELECT subthing FROM t2)
  AND tb.id IS NULL

(请确保将t1.id编入索引,并且两个表上的subthing索引也应该有帮助。)

答案 1 :(得分:1)

尝试左外连接:

    select distinct t1.id, t1.thing, t1.subthing, t1.tnumber from t1 inner join (SELECT t1.thing, subthing, max(t1.tnumber) as tnumber 
FROM t1 left outer join t2 using (subthing) where t2.subthing is NULL GROUP BY subthing, thing) as b using (subthing, thing, tnumber) ;

创建连锁索引:

create index index_name t1(subthing, thing, tnumber);

答案 2 :(得分:0)

尝试类似的东西?

SELECT *
FROM T1
WHERE CONCAT_WS('-', thing, `sub-thing`, tnumber) IN
(SELECT CONCAT_WS('-', thing, `sub-thing`, max_tnumber)
FROM (
    SELECT MAX(tnumber) AS max_tnumber, thing, `sub-thing`
    FROM T1
    WHERE T1.`sub-thing` NOT IN (SELECT `sub-thing` FROM T2)
    GROUP BY thing, `sub-thing`
) as view
);

答案 3 :(得分:0)

我发现遵循LFJ语法有一个问题:

SELECT t1.id, t1.thing, subthing, max(t1.tnumber) as tnumber
FROM t1 left outer join t2 using (subthing) where t2.subthing is NULL GROUP BY subthing, thing;

将导致

4       A1214       A01B00001     123459 

代替

6       A1214       A01B00001     123461 

id = 4是该子项的第一个ID,但是数字123461来自最大值,但最大值是记录ID = 6。

我认为语法应该从:

开始
SELECT max(t1.id), t1.thing (...)

答案 4 :(得分:0)

我基于@LFJ语法做了一些修改。

long int

结果相同,但比较是通过'changed `t1.id` column to `t1.pid`' 'changed `t2.id_table_t1` column to `t2.pid`' 'and JOINed tables via `pid` columns' SELECT DISTINCT t1.pid, t1.thing, t1.subthing, t1.tnumber FROM t1 INNER JOIN (SELECT t1.thing, t1.subthing, max(t1.tnumber) as tnumber FROM t1 LEFT OUTER JOIN t2 o ON t1.pid=o.pid WHERE o.pid IS NULL AND t1.added>'2015-10-31' GROUP BY t1.subthing, thing) as b using (subthing, thing, tnumber) ; 列(旧ID),而不是pid列,这意味着即使tnumber中的值是任何值,结果也会相同:)

我认为这是结束。也许它将来会对某人有所帮助。 非常感谢。