简体   繁体   English

MySQL 从基于 email 的表中获取 ID 然后使用 ID 和更多值在另一个表中创建一行

[英]MySQL get ID from a table based on email then create a row in another table using ID and more values

I have two tables users and features :我有两个表用户功能

users
+----+-----------------+
| id | email           |
+----+-----------------+
|  1 | test1@gmail.com |
|  2 | test2@gmail.com |
|  3 | test3@gmail.com |
|  4 | test4@gmail.com |
|  5 | test5@gmail.com |
+----+-----------------+

features
+------------+---------+---------------------+------------+
| feature_id | user_id | feature_name        | can_access |
+------------+---------+---------------------+------------+
|          1 |       1 | automated-investing |          1 |
|          2 |       1 | crypto              |          0 |
|          3 |       2 | crypto              |          0 |
|          4 |       3 | automated-investing |          0 |
|          5 |       4 | automated-investing |          1 |
|          7 |       1 | financial-tracking  |          1 |
|          8 |       2 | financial-tracking  |          0 |
|          9 |       3 | financial-tracking  |          1 |
|         10 |       4 | financial-tracking  |          0 |
+------------+---------+---------------------+------------+

I am trying to get the id from users based on email and then create a new row in features using that id , feature_name and can_access ( feature_name and can_access are from a request).我正在尝试从基于emailusers那里获取id ,然后使用该idfeature_namecan_accessfeatures中创建一个新行( feature_namecan_access来自请求)。

I am able to do it but it is split into two parts and I am looking to do it with a single query.我能够做到,但它分为两部分,我希望用一个查询来做到这一点。

Current query:当前查询:

// Get ID from email
SELECT id FROM users WHERE email="test5@gmail.com";
// Insert values into features 
INSERT INTO features(user_id, feature_name, can_access) VALUES(5,"crypto", 1);

Expected output:预期 output:

+------------+---------+---------------------+------------+
| feature_id | user_id | feature_name        | can_access |
+------------+---------+---------------------+------------+
|          1 |       1 | automated-investing |          1 |
|          2 |       1 | crypto              |          0 |
|          3 |       2 | crypto              |          0 |
|          4 |       3 | automated-investing |          0 |
|          5 |       4 | automated-investing |          1 |
|          7 |       1 | financial-tracking  |          1 |
|          8 |       2 | financial-tracking  |          0 |
|          9 |       3 | financial-tracking  |          1 |
|         10 |       4 | financial-tracking  |          0 |
|         11 |       5 | crypto              |          1 |
+------------+---------+---------------------+------------+
INSERT INTO features (user_id, feature_name, can_access) 
   SELECT id, "crypto", 1 FROM users WHERE email="test5@gmail.com";

So instead of specifying the values, you use the select statement.因此,您无需指定值,而是使用 select 语句。

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

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