简体   繁体   中英

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%

I have tried this LEFT JOIN. This query works when I have a value in t1, but not working when I have no value for the table 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

------------------------------------------------------------------------
   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

------------------------------------------------------------------------
   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. If it does not provide the result needed, let me know what is wrong.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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