简体   繁体   English

用于获取此查询的查询类型(左联接不起作用)

[英]What kind of query to use to get this (left join not working)

table : transmission
--------------------------------------------------------
   ID       ReqString          Timestamp          Actif
-------  -------------  ---------------------   --------
   a         O21         2016-05-02 10:03:27       1
   a         O20         2016-05-01 11:07:47       1
   a         O11         2016-05-02 09:27:53       1

   b         O20         2016-05-02 12:27:45       1
   b         O21         2016-05-01 09:32:55       1

I need to retrieve, for the same id, the latest values for ReqString LIKE O2% AND LIKE O1% 对于相同的ID,我需要检索ReqString LIKE O2%和LIKE O1%的最新值

I have tried this LEFT JOIN. 我已经尝试过此LEFT JOIN。 This query works when I have a value in t1, but not working when I have no value for the table t1... 当我在t1中有一个值时,此查询有效,但是当我在表t1中没有任何值时,该查询不起作用...

SELECT t1.ReqString AS O1, t2.ReqString AS O2, t1.Timestamp AS T1, t2.Timestamp AS T2
FROM transmission t1
LEFT JOIN transmission t2 ON t2.ID = t1.ID 
                         AND t2.ReqString LIKE 'O2%' 
                         AND t2.Actif=1
WHERE t1.ID = 'b' 
  AND t1.ReqString LIKE 'O1%'
  AND t1.Actif = 1
ORDER BY t1.Timestamp DESC, t2.Timestamp DESC
LIMIT 1

So if I run the query for the ID = 'a', I need to get 因此,如果我运行ID ='a'的查询,则需要获取

------------------------------------------------------------------------
   O1       O2               T1                         T2
-------  ---------  -----------------------   -------------------------
  O11       O21       2016-05-02 09:27:53        2016-05-02 10:03:27

and if I run it for the ID = 'b', the result I would like to have is 如果我为ID ='b'运行它,我想要的结果是

------------------------------------------------------------------------
   O1       O2               T1                         T2
-------  ---------  -----------------------   -------------------------
 NULL      O20              NULL                 2016-05-02 12:27:45
select t1.ReqString AS O1, t2.ReqString AS O2, t1.Timestamp AS T1, t2.Timestamp AS T2 from
(SELECT ReqString , Timestamp  
FROM transmission where ReqString LIKE 'O1%'  AND Actif=1 and ID = 'a'
limit 1 order by Timestamp DESC)t1,
(SELECT ReqString , Timestamp  
FROM transmission where ReqString LIKE 'O2%'  AND Actif=1 and ID = 'a'
limit 1 order by Timestamp DESC)t2

Try this: 尝试这个:

SELECT 
  result1.ReqString as 'O1',
  result2.ReqString as 'O2',
  result1.Timestamp as 'T1',
  result2.TimeStamp as 'T2'
FROM 
  (
    SELECT 
      @i:=@i+1 AS rowId,
      ReqString,
      Timestamp
    FROM transmission,(SELECT @i:=0) a 
    WHERE ReqString LIKE 'O1%'
    AND Actif=1 
    AND ID = 'a' 
    LIMIT 1 
    ORDER BY Timestamp DESC
  ) as result1

  LEFT JOIN 

  (
    SELECT
      @j:=@j+1 AS rowId,
      ReqString,
      Timestamp
    FROM transmission,(SELECT @j:=0) a 
    WHERE ReqString LIKE 'O2%'
    AND Actif=1 
    AND ID = 'a' 
    LIMIT 1 
    ORDER BY Timestamp DESC
  ) as result2 

  ON result1.rowId = result2.rowId; 

I have a feeling that left join may not be what you are looking for. 我觉得左联接可能不是您想要的。 This should produce the desired result whether or not there is a value in result1. 无论result1中是否有值,这都应该产生期望的结果。 If it does not provide the result needed, let me know what is wrong. 如果没有提供所需的结果,请告诉我哪里出了问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM