+ -
当前位置:首页 → 问答吧 → MYSQL语句问题

MYSQL语句问题

时间:2011-09-07

来源:互联网

insert into trainstation(trainID,stationID,arriveTime,startTime,runTime,distance,hard_seat,soft_seat,hard_sleep,soft_sleep) values('1043',select stationID from station where stationName ='奎屯','11:08','终点站','37小时43分','2810','143','-','280/291/303','469/492')


这个要怎么改才对啊stationID这个字段数据是另外张表查的

作者: imxinshou   发布时间: 2011-09-07

insert into trainstation(trainID,stationID,arriveTime,startTime,runTime,distance,hard_seat,soft_seat,hard_sleep,soft_sleep) values('1043',(select stationID from station where stationName ='奎屯'),'11:08','终点站','37小时43分','2810','143','-','280/291/303','469/492')
加个括号就行了,但要保证返回结果是一条记录,其它的语法我没看

作者: zengxianjunccccccccc   发布时间: 2011-09-07

select stationID from station where stationName ='奎屯'
这个SQL查出来的值可能不只一条,LZ这样写当然不对

LZ试试分开写SQL把,先写select stationID from station where stationName ='奎屯'获取ID,如果ID只有一个,就把ID作为参数执行insert

作者: jiangdaxian2003   发布时间: 2011-09-07

insert into trainstation(trainID,stationID,arriveTime,startTime,runTime,distance,hard_seat,soft_seat,hard_sleep,soft_sleep) values('1043',(select top 1 stationID from station where stationName ='奎屯'),'11:08','终点站','37小时43分','2810','143','-','280/291/303','469/492')

作者: cjg520jie   发布时间: 2011-09-07

这个问题大了。stationName ='奎屯' 有多条记录的

作者: imxinshou   发布时间: 2011-09-07

insert into trainstation(trainID,stationID,arriveTime,startTime,runTime,distance,hard_seat,soft_seat,hard_sleep,soft_sleep) values('1043',(select top 1 stationID from station where stationName ='奎屯'),'11:08','终点站','37小时43分','2810','143','-','280/291/303','469/492')

作者: jiangsuwx   发布时间: 2011-09-07

真对不起我没测试就说了,应该是这样
insert into trainstation(trainID,stationID,arriveTime,startTime,runTime,distance,hard_seat,soft_seat,hard_sleep,soft_sleep) select stationID '1043','11:08',..... from station where stationName ='奎屯'

作者: zengxianjunccccccccc   发布时间: 2011-09-07