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 问题


- 以一个值为轴心,但将一行上的数据按另一行分组? 2022-01-01
- 如何使用 pip 安装 Python MySQLdb 模块? 2021-01-01
- SQL 临时表问题 2022-01-01
- 远程 mySQL 连接抛出“无法使用旧的不安全身份验证连接到 MySQL 4.1+"来自 XAMPP 的错误 2022-01-01
- 导入具有可变标题的 Excel 文件 2021-01-01
- 使用 Oracle PL/SQL developer 生成测试数据 2021-01-01
- 在SQL中,如何为每个组选择前2行 2021-01-01
- 更改自动增量起始编号? 2021-01-01
- 如何将 Byte[] 插入 SQL Server VARBINARY 列 2021-01-01
- 如何将 SonarQube 6.7 从 MySQL 迁移到 postgresql 2022-01-01