求mysql 删除数据的语句。

飞翔的薄荷 2014-07-18 11:34:29
我有一个类似下面的数据库表。求删除语句。
ID name age Actionflag Action
1 李三 12 离开动作 关门
2 李三 12 进入动作 开门
3 李四 15 离开动作 喝水
4 李四 15 进入动作 到水
5 李三 15 离开动作 喝水
6 李三 15 进入动作 到水

第一条记录和第二条记录同时插入,第三条和第四条也是同时插入。现在知道其中一个id比如1,那么我想删除第一条和第二条,用1条sql语句该如何做。注意:知道一个id,根据name age 两个字段可以唯一确认另一个。

当时设计数据库时,本来是 类似下面的设计:
ID name age Action1 Action2
但是评审不通过,我的领导不同意这样的设计,哎。
...全文
234 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
天堂的鸽子 2014-07-28
  • 打赏
  • 举报
回复
呵呵,我初学的,后来燕子,select可以但是delete不行。
SyaNHs 2014-07-27
  • 打赏
  • 举报
回复
引用 5 楼 TIANTANGDEGEZI 的回复:
[quote=引用 2 楼 ml232528 的回复:] 测试了这个语句,但是不可用。 delete from testtable where name = (select name from testtable where id = 1) and age = (select age from testtable where id = 1) Error Code: 1093. You can't specify target table 'testtable' for update in FROM clause
不知道楼主用的说明编辑器啊,我测试了一下没有报错啊! [/quote] 印象里MySql不能这样自参照的吧,你用的是否是MSSQL?
天堂的鸽子 2014-07-23
  • 打赏
  • 举报
回复
引用 2 楼 ml232528 的回复:
测试了这个语句,但是不可用。

delete from testtable where name = (select name from testtable where id = 1) and age = (select age from testtable where id = 1)

Error Code: 1093. You can't specify target table 'testtable' for update in FROM clause


不知道楼主用的说明编辑器啊,我测试了一下没有报错啊!
天堂的鸽子 2014-07-23
  • 打赏
  • 举报
回复
下面方法也是可以的:
create table testtable
(id  int,
sname char(10),
age int,
ActionFlage char(20),
Actions char(20));

insert into testtable values(
1,'李三',12,'离开动作','关门'),
(2,'李三',12,'进入动作','开门'),
(3,'李四',15,'离开动作','到水'),
(4,'李四',15,'进入动作','开门'),
(5,'李三',15,'离开动作','喝水'),
(6,'李三',15,'进入动作','到水');

select * from testtable;

select * from testtable where id=1;

delete  from testtable where sname+age in
(select distinct sname+age from testtable where id=1);
飞翔的薄荷 2014-07-19
  • 打赏
  • 举报
回复
自己解决了: delete from testtable where name = (select a.name from (select * from testtable where id = 1) as a) and age = (select b.age from (select * from testtable where id = 1) as b)
飞翔的薄荷 2014-07-19
  • 打赏
  • 举报
回复
测试了这个语句,但是不可用。 delete from testtable where name = (select name from testtable where id = 1) and age = (select age from testtable where id = 1) Error Code: 1093. You can't specify target table 'testtable' for update in FROM clause
ACMAIN_CHM 2014-07-18
  • 打赏
  • 举报
回复
delete from 有一个类似下面的数据库表 where ID=1 or ID=2

56,681

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧