I am trying to go through my users table, and if the rank column is a certain value, then I want to insert a row into the messages table, with one column, "to", being the username column from the users table, and the rest of the columns in the inserted row need to be a predefined value. I'm having trouble making this work- any help?
MySQL Configuration: (Those which are needed)
Users table:
id|username|rank
Messages table:
id|to|from|body|subject
( to
needs to be from the users
table, from
, body
,and subject
need to be a predefined string
I tried to figure something out like this, but it's not just quite all I need it to do. I got this code and modified it from another Stack Overflow question that was similar to this one.
INSERT INTO `messages` (`from`,`to`,`subject`,`body`)
SELECT `id`,`rank`
FROM `users`
WHERE `rank`='Super'
You can use string literals enclosed in single quotes as column values. It is safe to intermix them with the actual column values from the rows returned by the SELECT
portion of your INSERT INTO...SELECT
statement.
INSERT INTO `messages` (`from`, `to`, `subject`, `body)
SELECT
/* single-quote string literals to use them in the INSERT INTO...SELECT */
'Predefined string From',
/* Intermixed with column values from the rows returned by the SELECT */
`username`,
'Predefined string subject',
'Predefined string body'
FROM `users`
WHERE `rank`='Super'
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.