简体   繁体   中英

MySQL INSERT if SELECT COUNT(*) is greater than 1

I'm trying a MySQL statement, where I need to check of the user credentials are correct; and only if they are correct -- I insert the data

IF (SELECT COUNT(*) FROM users WHERE uid = 1 AND password = "67^8ax%!") > 0
THEN
INSERT INTO messages (uid, text, time)
VALUES (1, "Hello", CURRENT_TIMESTAMP)
END IF

I have to do both the operations in a single MySQL query due the limitations of my platform.

Any way to get this working?

Try this query

Working in MYSQL, thought shouldn't work in MYSQL :P

INSERT INTO messages (uid, text, time)
(SELECT 1, "Hello", CURRENT_TIMESTAMP FROM users WHERE uid = 1 AND password = "67^8ax%!")

Will work if there is only 1 distinct user with the given username and password..

FIDDLE

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM