2

I am trying to insert value into mysql 5.1 table using the following statement

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'
)

But I am getting syntax error MySQL server version for the right syntax to use near '`),'110','70')' at line 2 (sorry for not putting this into code but it is not working because of apostrophes used). What am I doing wrong? I have tried some other solution from stackoverflow but I cant get them working

Thanks in forward

EDIT: this is the correct version of my statement

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'
)

both these and accepted answer works so feel free to use any of them

2 Answers 2

3

Try this syntax

Add the constant values to the select column list in order of insert column list

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 
Sign up to request clarification or add additional context in comments.

2 Comments

actualy even my solution is working I just forget to remove one apostrophe I will post correct version too
@horin - unless your sub-query returns more than one row.
0

Modified Insert statement as:

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.