简体   繁体   English

如果SELECT COUNT(*)大于1,则MySQL INSERT

[英]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; 我正在尝试MySQL语句,我需要检查用户凭据是否正确; 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. 由于我的平台的限制,我必须在单个MySQL查询中执行这两个操作。

Any way to get this working? 有什么办法让这个工作?

Try this query 试试这个查询

Working in MYSQL, thought shouldn't work in MYSQL :P 在MYSQL中工作,思想不应该在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 小提琴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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