大家好,我正在尝试弄清如何使用以下查询插入新记录:
SELECT user.id, user.name, user.username, user.email,
IF(user.opted_in = 0, 'NO', 'YES') AS optedIn
FROM
user
LEFT JOIN user_permission AS userPerm ON user.id = userPerm.user_id
ORDER BY user.id;
到目前为止,我的INSERT
查询是这样的:
INSERT INTO user
SELECT *
FROM user
LEFT JOIN user_permission AS userPerm ON user.id = userPerm.user_id;
然而,我不确定在使用左连接和内连接时如何执行VALUE('','','','',等等)
。
因此,我要做的是:
User
表:
id | name | username | password | OptIn
--------------------------------------------------------------------
562 Bob Barker bBarker BBarker@priceisright.com 1
还有 user_permission
表
user_id | Permission_id
-------------------------
562 4
更新 像这样吗?
INSERT INTO user (name, username, password, email, opted_in) VALUES ('Bbarker','Bbarker','blahblahblah','Bbarker@priceisright.com',0);
INSERT INTO user_permission (user_id, permission_id) VALUES (LAST_INSERT_ID(),4);
user
中选择了值,为什么要插入到user
中呢?你打算插入哪一行新数据? - Michael Berkowski