MySQL插入值(选择)

时间:2015-12-27 07:43:12

标签: mysql

我正在尝试使用以下语句将值插入到mysql 5.1表中

INSERT INTO `bp_measurements`
(`id_patient`, `time`, `location`, `systolic`, `diastolic`) 
VALUES (
'2',
'2015-12-26 13:19:35',
(SELECT `id` FROM `gps_locations` WHERE `lon` = 20.40930 AND `lat` = 48.94990`),
'110',
'70'
)

但是我得到了MySQL服务器版本的语法错误,因为正确的语法可以在'`,' 110' 70' 70'''在第2行(很抱歉没有把它放到代码中但由于使用了撇号而无法正常工作)。我究竟做错了什么?我已尝试过stackoverflow的其他解决方案,但我无法让它们正常工作

先谢谢

修改 这是我的陈述的正确版本

INSERT INTO `bp_measurements`
(`id_patient`, `time`, `location`, `systolic`, `diastolic`) 
VALUES (
'2',
'2015-12-26 13:19:35',
(SELECT `id` FROM `gps_locations` WHERE `lon` = 20.40930 AND `lat` = 48.94990),
'110',
'70'
)

这些和接受的答案都可以随意使用其中任何一个

2 个答案:

答案 0 :(得分:3)

试试这个syntax

按插入列列表的顺序将常量值添加到select列列表

INSERT INTO `bp_measurements` 
            (`id_patient`, 
             `time`, 
             `location`, 
             `systolic`, 
             `diastolic`) 
SELECT '2', 
       '2015-12-26 13:19:35', 
       `id`, 
       '110', 
       '70' 
FROM   `gps_locations` 
WHERE  `lon` = 20.40930 
       AND `lat` = 48.94990 

答案 1 :(得分:0)

将Insert语句修改为:

INSERT INTO bp_measurements(id_patient, time, location, systolic, diastolic) (SELECT '2' AS id_patient,'2015-12-26 13:19:35' AS time,id AS location,'110' AS systolic,'70' AS diastolic FROM gps_locations WHERE lon = 20.40930 AND lat = 48.94990)