line_state表的两个字段关联同一个外键位,我应该怎么查询才能让两个字段都能与外键关联

drop table if exists line_state;

CREATE TABLE `line_state` (

`id` int(11) NOT NULL AUTO_INCREMENT,

`fromCityId` int(11),

`toCityId` int(11),

`value` int(11),

PRIMARY KEY (`id`),

foreign key(fromCityId) references city(id),

foreign key(toCityId) references city(id)

);

CREATE TABLE `city` (

`id` int(11) NOT NULL,

`name` VARCHAR(20) NOT NULL,

PRIMARY KEY (`id`)

)DEFAULT CHARSET=utf8;

select ls.id, c.name ,ls.value, ls.toCityId from line_state ls inner join city c on ls.fromCityId=c.id order by ls.id asc;

我现在的这个ls.fromCityId 可以与外键连接变成 中文字,但是ls.toCityId怎么变成中文字?

图片描述

回答:

select ls.id, c.name ,ls.value, ls.toCityId ,d.name from line_state ls inner join city c on

                                                                         ls.fromCityId=c.id

inner join city d on

ls.toCityId=d.id

order by ls.id asc;

以上是 line_state表的两个字段关联同一个外键位,我应该怎么查询才能让两个字段都能与外键关联 的全部内容, 来源链接: utcz.com/a/166173.html

回到顶部