简体   繁体   English

Mysql关节是可能的吗?

[英]Mysql joints is possible?

I have two table one table containing station id and station name, and another table containing id, name, startStationId, endStationId. 我有两个表一个表包含站ID和站名,另一个表包含id,name,startStationId,endStationId。 I know second table's id by using that id i need to fetch all details of second table with station name for corresponding 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
-------------------------------------------

For example i know second table id(123).. so i want to fetch all results by using id, result would be. 例如,我知道第二个表id(123)..所以我想通过使用id获取所有结果,结果将是。

Id =>123, Name => Dhinesh, StartStaion =>Chennai , Endstation=>Mumbai. Id => 123,Name => Dhinesh,StartStaion => Chennai,Endstation =>孟买。

How can we write in one single query...? 我们如何在一个查询中写...?

Thanks in advance. 提前致谢。

Try this. 尝试这个。

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

this should work 这应该工作

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

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