SQL 多个带条件的语句
SQL multiple statements with conditions
我正在尝试在一个验证序列查询中整理 SQL 个语句。
用户
╔════╦══════════════╦══════════╗
║ id ║ emailAddress ║ password ║
╠════╬══════════════╬══════════╣
║ 1 ║ test1 ║ pass1 ║
║ 2 ║ test2 ║ pass2 ║
║ 3 ║ test3 ║ pass3 ║
╚════╩══════════════╩══════════╝
user_token
╔═══════╦═════════╗
║ token ║ user_id ║
╠═══════╬═════════╣
║ t1 ║ 1 ║
║ t2 ║ 2 ║
║ t3 ║ 3 ║
╚═══════╩═════════╝
- 检查记录是否存在以及电子邮件地址和密码
- 如果找到匹配项,则将带有 user_id 的标记插入 user_token table
- 返回resultSet中的用户记录
我的尝试(部分) 请注意,我可以组合多个 sql 由 ;
分隔的语句
条件插入
INSERT INTO user_token(token, user_id)
SELECT ?, ? FROM DUAL
WHERE EXISTS (SELECT * FROM user WHERE emailAddress = ? AND password = ?);
["t1", 1, "test1", "pass1"];
设置某种变量,尝试结合其他尝试。
SELECT @id:=id, emailAddress, password FROM user WHERE emailAddress = "test1" AND password = "pass1";
我想我已经接近了,但是从子查询外部访问 @id 时遇到问题。
serviceRequest.sql = "INSERT INTO user_token(token, user_id)\
SELECT ?, @id FROM DUAL WHERE EXISTS (
SELECT @id:=id, emailAddress, password FROM user WHERE emailAddress = ? AND password = ?
);
SELECT @id, ? FROM DUAL";
serviceRequest.values = ["t1", "test1", "pass1", "t1"];
Objective:
在 SQL 中的 IF
子句和用户定义变量的帮助下,将多个 SQL 语句组合在单个 transaction/execution 中使用逻辑来实现身份验证过程等等
也许您正在寻找 last_insert_id()
和 row_count()
。为了安全起见,我会将这些分配给变量:
INSERT INTO user_token(token, user_id)
SELECT ?, ? FROM DUAL
WHERE EXISTS (SELECT * FROM user WHERE emailAddress = ? AND password = ?);
SELECT @row_count := ROW_COUNT(), @last_insert_id = LAST_INSERT_ID();
SELECT u.*
FROM user
WHERE @row_count > 0 AND emailaddress = ? and password = ?;
我不确定这是否正是您想要的,但这确实显示了您可能需要的两个函数的用法。
终于破解了它,但我需要改进查询 #2,使用 IF 而不是在 WHERE EXISTS
中执行另一个 SELECT
语句 任何人?
serviceRequest.sql = "SET @emailAddress = ?, @password = ?;\
SELECT @authToken:= ? AS authToken, @id:=id AS id FROM user WHERE emailAddress = @emailAddress AND password = @password;\
INSERT INTO user_token(authToken, user_id) \
SELECT @authToken, @id FROM DUAL WHERE EXISTS (SELECT id FROM user WHERE emailAddress = @emailAddress AND password = @password);";
serviceRequest.values = [request.body.credentials.emailAddress, request.body.credentials.password, "t3"];
查询 #1。检索到第一条用户记录并设置变量(@token
、@id
)并作为结果集返回。
查询 #2。 user_token 如果记录存在,插入将执行
如果有人可以改进这个答案并进一步优化查询,特别是查询 #2,我有另一个 select 语句,我相信 WHERE EXISTS
可以利用检查 @id
不是 null 插入发生,我不必在 values
数组中传递 username/password 两次。
我正在尝试在一个验证序列查询中整理 SQL 个语句。
用户
╔════╦══════════════╦══════════╗
║ id ║ emailAddress ║ password ║
╠════╬══════════════╬══════════╣
║ 1 ║ test1 ║ pass1 ║
║ 2 ║ test2 ║ pass2 ║
║ 3 ║ test3 ║ pass3 ║
╚════╩══════════════╩══════════╝
user_token
╔═══════╦═════════╗
║ token ║ user_id ║
╠═══════╬═════════╣
║ t1 ║ 1 ║
║ t2 ║ 2 ║
║ t3 ║ 3 ║
╚═══════╩═════════╝
- 检查记录是否存在以及电子邮件地址和密码
- 如果找到匹配项,则将带有 user_id 的标记插入 user_token table
- 返回resultSet中的用户记录
我的尝试(部分) 请注意,我可以组合多个 sql 由 ;
条件插入
INSERT INTO user_token(token, user_id) SELECT ?, ? FROM DUAL WHERE EXISTS (SELECT * FROM user WHERE emailAddress = ? AND password = ?); ["t1", 1, "test1", "pass1"];
设置某种变量,尝试结合其他尝试。
SELECT @id:=id, emailAddress, password FROM user WHERE emailAddress = "test1" AND password = "pass1";
我想我已经接近了,但是从子查询外部访问 @id 时遇到问题。
serviceRequest.sql = "INSERT INTO user_token(token, user_id)\ SELECT ?, @id FROM DUAL WHERE EXISTS ( SELECT @id:=id, emailAddress, password FROM user WHERE emailAddress = ? AND password = ? ); SELECT @id, ? FROM DUAL"; serviceRequest.values = ["t1", "test1", "pass1", "t1"];
Objective:
在 SQL 中的 IF
子句和用户定义变量的帮助下,将多个 SQL 语句组合在单个 transaction/execution 中使用逻辑来实现身份验证过程等等
也许您正在寻找 last_insert_id()
和 row_count()
。为了安全起见,我会将这些分配给变量:
INSERT INTO user_token(token, user_id)
SELECT ?, ? FROM DUAL
WHERE EXISTS (SELECT * FROM user WHERE emailAddress = ? AND password = ?);
SELECT @row_count := ROW_COUNT(), @last_insert_id = LAST_INSERT_ID();
SELECT u.*
FROM user
WHERE @row_count > 0 AND emailaddress = ? and password = ?;
我不确定这是否正是您想要的,但这确实显示了您可能需要的两个函数的用法。
终于破解了它,但我需要改进查询 #2,使用 IF 而不是在 WHERE EXISTS
中执行另一个 SELECT
语句 任何人?
serviceRequest.sql = "SET @emailAddress = ?, @password = ?;\
SELECT @authToken:= ? AS authToken, @id:=id AS id FROM user WHERE emailAddress = @emailAddress AND password = @password;\
INSERT INTO user_token(authToken, user_id) \
SELECT @authToken, @id FROM DUAL WHERE EXISTS (SELECT id FROM user WHERE emailAddress = @emailAddress AND password = @password);";
serviceRequest.values = [request.body.credentials.emailAddress, request.body.credentials.password, "t3"];
查询 #1。检索到第一条用户记录并设置变量(@token
、@id
)并作为结果集返回。
查询 #2。 user_token 如果记录存在,插入将执行
如果有人可以改进这个答案并进一步优化查询,特别是查询 #2,我有另一个 select 语句,我相信 WHERE EXISTS
可以利用检查 @id
不是 null 插入发生,我不必在 values
数组中传递 username/password 两次。