MYSQL INSERT SELECT problem(MYSQL INSERT SELECT 问题)
问题描述
我在理解如何执行某些 INSERT SELECT 方面有点困难.
i have a little difficulty in understanding how to do some INSERT SELECT.
例如我有两张桌子.
TABLE : users
id | name | gender
1 | John | m
2 | Mary | f
TABLE : website
fid | url | id
1 | www.desilva.biz | 2
2 | gidhelp.com | 4
现在假设我想向表格网站添加另一个查询.我得到两个变量,让我们说:
Now let's say i want to add another query to the table website. I get two variables, lets say:
$user = John;
$site = "www.google.com";
我想从 users 表中选择 John 的 id 并在一个语句中将其插入到 website 表中.
i want to select the id of John from users table and insert it into website table in one statement.
我该怎么做?
推荐答案
假设你的变量已经被正确转义并且不受SQL注入的影响:
Assuming your variables are already escaped properly and are not subject to SQL injection:
INSERT
INTO website (url, fid)
SELECT $site, id
FROM users
WHERE name = $user
这篇关于MYSQL INSERT SELECT 问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:MYSQL INSERT SELECT 问题
基础教程推荐
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
