嘿,我想弄清楚如何使用以下查询插入新记录:
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('','','','', etc etc)
在使用左连接和内连接时该怎么做。
所以我想做的是:
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);