0

我以前做过一次,但不记得是怎么做到的……我已经为此苦苦挣扎了,现在正在寻求帮助。

我有一个包含两列的表:Uuid 和 ProcessId ProcessId 列当前有两个值:ValueA 和 ValueB

我想做一个自连接(完全外连接?),如下表:

        第1234章
        第1234章
        第2345章
        第3456章

我将得到如下结果:

        Uuid 值A 值B
        1234 好的 好的
        第2345章
        3456 OK 丢失
4

2 回答 2

1

尝试这个:

SELECT Uuid, 
   CASE WHEN U.ValueA>0 THEN 'OK' ELSE 'Missing' END AS ValueA,
   CASE WHEN U.ValueB>0 THEN 'OK' ELSE 'Missing' END AS ValueB
FROM 
 (SELECT T.Uuid, 
     (SELECT COUNT(*) FROM MyTable AS M WHERE T.Uuid=M.UserID AND ValueField='ValueA') AS ValueA, 
     (SELECT COUNT(*) FROM MyTable AS M WHERE T.Uuid=M.UserID AND ValueField='ValueB') AS ValueB
  FROM (SELECT DISTINCT Uuid FROM MyTable) AS T
  GROUP BY Uuid) AS U
ORDER BY Uuid
于 2012-07-02T22:32:59.793 回答
0

根据索引、表大小和优化器,使用一对自连接可能是最快的:

select TableU.Uuid, 
    if(count(TableA.ProcessId) > 0, 'OK', 'Missing') as ValueA, 
    if(count(TableB.ProcessId) > 0, 'OK', 'Missing') as ValueB 
from Table11301861 as TableU
left join Table11301861 as TableA on TableA.ProcessId = 'ValueA' and TableA.Uuid = TableU.Uuid
left join Table11301861 as TableB on TableB.ProcessId = 'ValueB' and TableB.Uuid = TableU.Uuid
group by TableU.Uuid
于 2012-07-03T15:25:15.620 回答