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 问题
基础教程推荐
- 如何使用 mysql.connector 禁用查询缓存 2022-01-01
- SQL:使用来自具有相同列名的两个表中的数据... 2021-01-01
- SQL Server 实例在登录协商期间返回无效或不受支持的协议版本 2021-01-01
- 无法解决整理冲突 2021-01-01
- SQL 效率:WHERE IN 子查询 vs. JOIN 然后 GROUP 2021-01-01
- 在 SQL 中连接多个表 2021-01-01
- 需要 MySQL 5.1 中的抽象触发器来更新审计日志 2021-01-01
- 将 SQL Server DateTime 列迁移到 DateTimeOffset 2021-01-01
- SSMS 中的权限问题:“对象 'extended_properties'、数据库 'mssqlsystem_resource'、... 错误 229)上的 SELECT 权限被拒绝" 2022-01-01
- 是否可以执行按位分组功能? 2021-01-01
