如何离开联接表并保持顺序?

时间:2020-02-28 09:03:01

标签: sql

我试图像这样连接这两个表:

select 
    T2.* 
from 
    (SELECT DISTINCT DésignationBDC2 FROM pdc2 where BDC3 like '%01|06|02%') AS T1
    LEFT JOIN (select DésignationBDC2, COULEUR from pdc2 where Id=60) AS T2 ON T1.DésignationBDC2 = T2.DésignationBDC2

这些表是:

enter image description here

enter image description here

但是,我明白了:

enter image description here

并且您将看到,“DésignationBDC2”的顺序从第一个表中丢失。 我如何避免这种情况?

1 个答案:

答案 0 :(得分:2)

您不需要使用子查询即可left join

SELECT DISTINCT C2.DésignationBDC2, C22.COULEUR  
FROM pdc2 AS C2 LEFT JOIN
     pdc2 AS C22
     ON C22.DésignationBDC2  = C2.DésignationBDC2 AND C22.ID = 60
WHERE P2.BDC3 like '%01|06|02%'
ORDER BY C2.DésignationBDC2;