簡體   English   中英

Mysql需要從table1中獲取一列兩次,然后通過table2的兩個不同列對它們進行排序

[英]Mysql need to get one column two times from table1 and sort them by two different columns of table2

事情就是這樣,我有兩個表:

  • table1具有intUsersID, varUsersName

  • table2有列intCouriers, intResponsible

intCouriers (具有一些Couriers的intUsersID)和intResponsible (具有一些負責任的intUsersID)

在我的查詢中,我必須看到Couriers和負責人的用戶名是這樣的:

SELECT 
    table1.varUsersName 'Couriers',
    table1.varUsersName 'Responsible'
FROM
    table1 
LEFT JOIN 
    table2 ON table2.intCouriers = table1.intUsersID

然后,我需要一些如何子查詢或加入“ table1.varUsersName'Responsible'”的方法,以同時獲得“ Reponsible”人員。 請幫我。

應該是這個

SELECT table1.varUsersName 'Couriers', table2.varUsersName 'Responsible'
FROM table1 
INNER JOIN table3  on table1.intUsersID = table3.intCouriers
INNER JOIN table1  as Table2 on  table2.intUsersID = table3. intResponsible
SELECT Couriers.varUsersName as "Couriers",
       Responsible.varUsersName as "Responsible"
FROM   `table2` t2
   LEFT JOIN table1 Couriers on Couriers.intUsersID = t2.intCouriers
   LEFT JOIN table1 Responsible on Responsible.intUsersID = t2.intResponsible

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM