I would like to create a query to insert several values along with values that come from other tables, for example:
INSERT INTO table
(date, user_id, category_id, title, description)
VALUES
((STR_TO_DATE('04-01-2014', '%b-%d-%Y'),
(SELECT id FROM users WHERE username = 'admin' LIMIT 1),
(SELECT id FROM categories WHERE category = 'games' LIMIT 1),
'title here',
'description here')
This however, gives me an error and from PHP a 500 internal server error. Help please.
INSERT INTO table (fields)
SELECT
str_to_date(),
users.id,
categories.id,
'title',
'description'
FROM
users
INNER JOIN
categories ON users.key = categories.key
WHERE
users.username = 'admin' AND
categories.category = 'games'
that should do it I think...
I'm pretty sure you need a SELECT
after VALUES
if you're doing it this way. Try this and see if it works.
INSERT INTO table
(date, user_id, category_id, title, description)
VALUES
SELECT
STR_TO_DATE('04-01-2014', '%b-%d-%Y'),
(SELECT id FROM users WHERE username = 'admin' LIMIT 1),
(SELECT id FROM categories WHERE category = 'games' LIMIT 1),
'title here',
'description here'
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.