简体   繁体   English

如何 select 连接表中的两行

[英]How to select two rows from a joined table

So i have two tables with information below one which is a holiday table which marks the location to and from and etc. and another table that has the information on the locations所以我有两个表,下面有一个信息表,一个是假期表,它标记了往返的位置等,另一个表有位置信息

holiday_id假日编号 location_from location_from location_to location_to start_time开始时间 end_time时间结束
1 1个 21 21 43 43 13:00 13:00 17:00 17:00
Location_id Location_id location_name地点名称
21 21 sydney悉尼
43 43 gold coast黄金海岸

I'd like to have an sql query which will return the names of the location to and from but because location name is under one column im not sure how to do it.我想要一个 sql 查询,它将返回往返位置的名称,但因为位置名称在一列下,所以我不确定该怎么做。 any help is appreciated任何帮助表示赞赏

You can join with the table, twice, using an alias:您可以使用别名两次加入表格:

select location_1.location_name, location_2.location_name, other, columns
from holidays
join locations location_1 on holidays.location_from = location_1.location_id
join locations location_2 on holidays.location_to   = location_2.location_id
select a.location_name,b.location_name from table1 
left join table2 on table2.Location_id = table1.location_from
left join table2 on table2.Location_id = table1.location_to

Just need to join to the location table twice.只需要加入位置表两次。 Once for the location_from and again for the location_to:一次用于 location_from,一次用于 location_to:

SELECT
    h.holiday_id,
    h.location_from,
    locfrom.location_name,
    h.location_to,
    locto.locatin_name,
    h.start_time,
    h.end_time
FROM    Holidays h
INNER JOIN Locations AS locfrom
    ON locfrom.Location_id = h.location_from
INNER JOIN Locations locto
    ON locto.Location_id = h.location_to;

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

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