簡體   English   中英

Mysql關節是可能的嗎?

[英]Mysql joints is possible?

我有兩個表一個表包含站ID和站名,另一個表包含id,name,startStationId,endStationId。 我知道第二個表的ID是通過使用該ID我需要獲取第二個表的所有詳細信息,其中包含相應的startStationId,endStationId的站名。

ex: table1
---------------------------------
slNo staionId staionName
---------------------------------
1     0012     Bangalore ,
2     0014     Chennai ,
3     0015     Mumbai
---------------------------------

Table 2:
------------------------------------------
Id   Name    startStationId   endStationId
-------------------------------------------
123  Dhinesh   0014             0015
-------------------------------------------

例如,我知道第二個表id(123)..所以我想通過使用id獲取所有結果,結果將是。

Id => 123,Name => Dhinesh,StartStaion => Chennai,Endstation =>孟買。

我們如何在一個查詢中寫...?

提前致謝。

嘗試這個。

SELECT t2.Id,t2.name,t1.StationName,t3.StationName
FROM table2 t2
INNER JOIN table1 t1 ON t2.startStationId = t1.stationId
INNER JOIN table1 t3 ON t2.endStationId = t3.stationId
SELECT t2.Id, t2.Name, tstart.stationName , tend.stationName 
FROM table2 as t2 
INNER JOIN table1 as tstart  ON t2.startStationId =  tstart.stationId
INNER JOIN table1 as tend  ON t2.endStationId = tend.stationId

這應該工作

暫無
暫無

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

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