• 首页
  • 狐文
  • 狐图
  • About
狐窝
OvO
  1. 首页
  2. 未分类
  3. 正文

一条SQL查询语句的问题

2017年08月06日 72点热度 0人点赞 0条评论

2009 from---https://q.cnblogs.com/q/9074/

昨天通过查询分析器批量删除垃圾用户的时候,结果把用户表给清空了,上百万条记录没了。

经过半小时的数据库还原,问题解决。用户数据都完全恢复。

但是那条语句为什么把用户表清空了我倒是很迷糊。

那条语句是这样的

1.delete Table_1 where UserID in (select [ID] from Table_2)

问题是Table_2没有[ID]字段。结果Table_1的数据都清空了。

于是我做了下面的测试

1.select [ID] from Table_2

报错 列名 ‘ID’ 无效。

1.select top 10 * from Table_1 where UserID in (select [ID] from Table_2)

列出了10条记录。

再测试,把[ID]修改为[xxxxx]

1.select top 10 * from Table_1 where UserID in (select [xxxxx] from Table_2)

报错 列名 ‘xxxxx’ 无效。

看来是[ID]的问题,不明白这是为什么。

请大家帮忙理解一下这个问题,谢谢了。

 

数据库 SQL Server
Hoky的主页 Hoky | 初学一级 | 园豆:50
提问于:2009-08-28 14:53
< > 找找看

分享
最佳答案
0

当Table2里没有ID,用的就是Table1的ID,以后小心

收获园豆:80
Gray Zhang | 专家六级 |园豆:17610 | 2009-08-28 15:36
其他回答(6)
0

刚才做了一下试验,

where UserID in (select [xxxxx] from Table_2)

[xxxxx]字段在table_1表里存在,但table_2表里不存在,语句没有错误,

但[xxxxx]在两个表里都不存在,语句提示错误。

也就是说[xxxxx]这个字段只要是在两个表里都存在,就是正确的。

create table t1
(
Id int identity(1,1),
name varchar(20)
)

create table t2
(
t2Id int identity(1,1),
nam varchar(20)
)

insert t1 select 'A'
insert t1 select 'B'
insert t1 select 'C'
insert t1 select 'D'
insert t1 select 'E'
insert t1 select 'F'

select * from t1 where name in (select name from t2)

收获园豆:10
Annie | 园豆:396 (菜鸟二级) | 2009-08-28 15:05
0
 create table table01
(
 Id int identity(1,1),
 name varchar(20)
)

create table table02
(
 t2Id int identity(1,1),
 nam varchar(20)
)


insert table01 select 'A'
insert table01 select 'B'
insert table01 select 'C'
insert table01 select 'D'
insert table01 select 'E'
insert table01 select 'F'

select * from table01 where name in (select name from table02)
(0 row(s) affected)

delete from table01 where name in (select name from table02)
(0 row(s) affected)

insert table01 select null

select * from table01 where name in (select name from table02)
(0 row(s) affected)
delete from table01 where name in (select name from table02)
(0 row(s) affected)


select * from table01 where Id in (select id from table02)
(0 row(s) affected)
delete from table01 where Id in (select id from table02)
(0 row(s) affected)

没出现你说的状况,sql2005 sp3/sql2008/sql2000 sp4下结论相同

 

是否你在删除的时候忘了where条件?或者当时只选中前面部分没选where??

邀月 | 园豆:25175 (高人七级) | 2009-08-28 15:51
0

经过验证

Gray Zhang:当Table2里没有ID,用的就是Table1的ID,以后小心

正解。原来还有这样的啊。我也是第一次碰到这个怪问题。学习!

邢少 | 园豆:11116 (专家六级) | 2009-08-28 16:19
0

估计设置个别名会安全一些。。。

IS黑山老妖 | 园豆:550 (小虾三级) | 2009-08-28 16:36
0

同意Gray Zhang 的说法Table2里没有ID,用的就是Table1的ID,以后小心。

刚才在测试的时候发现,如果Table2表里面没有任何数据的时候,如邀月 发的那样,是没有找到任何数据的。

只要在Table2里面有一条数据,使用

Code

将会把所有table01表的数据检索出来。

收获园豆:10
可易信息技术 | 园豆:239 (菜鸟二级) | 2009-08-28 17:12
0

长见识了····

skyedge | 园豆:275 (菜鸟二级) | 2009-08-28 19:36
标签: 暂无
最后更新:2017年08月06日

OvO

狐狸

点赞
< 上一篇
下一篇 >
最新 热点 随机
最新 热点 随机
钛备份或小米系统备份功能备份出来的APK无法正常还原 BBR安装启用 安装docker mitmproxy 调试方法 Android SDK 版本选择 Perdition Mail代理服务器配置 HAProxy SSL代理 Openssl 中间人SSL代理 Stunnel SSL to SSL代理 NGINX反代IMAP配置 各种设备User Agent Torrent下载工具推荐 用 rar2john+hashcat 破解 RAR 文件密码 2020最好的Linux发行版 关闭新版chrome工具栏上拼图图标 Office 下载、安装、激活,有它就够了! Android 非官方的发行版 openwrt添加autoconf-archive
SQL Server中利用正则表达式替换字符串的方法 添加SCOTT实例步骤 Openssl 中间人SSL代理 使用Python做验证码识别 LinuxMint18安装中文输入法 Caddy文件存储位置 如何更改安卓屏幕分辨率? cwRsync设置及使用 Linux入门之内核管理番外篇(4)udev入门(1) 在Centos7上安装Htop Linux关闭IPv6 Linux VPS主机安全 Linux Mint 安装JDK 创建不可登录的服务账号及启动定制服务 脑洞大开的自然语言验证码 Git如何使用代理 mybatis中使用map类型参数,其中key为列名,value为列值 Windows去重服务如何工作
标签聚合
e chromium docker chrome 路由 镜像 密码 下载 blog 安装 网卡 ssl linux git https yum

COPYRIGHT © 2020 狐窝. ALL RIGHTS RESERVED.

THEME KRATOS MADE BY VTROIS