如何删除元组,包括下级元组。数据库

mutoujuelian 2010-01-07 03:52:23
我表述不是很清楚,勿怪。

属性栏:ID(主键), STEP。

情况是这样子的。。我想删除一个元组。比如说吧:是菜单栏目里面Step为100001的元组,其ID是随机生成的

在它的下面的子组织有Step为100001001和100001002的元组。。。

请问如何写命令使我在删除Step为100001元组的时候,连并下面的子组织一起删除。。

如何实现。用LIKE还是??

我是要得出这样的效果 delete from A where ID=? 并非是直接删除以上3个值。
...全文
253 27 打赏 收藏 转发到动态 举报
写回复
用AI写文章
27 条回复
切换为时间正序
请发表友善的回复…
发表回复
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
谢谢高手,解决了。不过你能不能给我讲解下这个语句的意思啊,instra我从来没见过啊。。你写的这种DELETE语句我也没见过。。麻烦讲解下。。万分感谢![Quote=引用 22 楼 wwwwb 的回复:]
MYSQL不支持
delete from  tab where step like concat((select Step from tab where id='123123'),'%')
这种写法,其它数据库支持
[/Quote]
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
这个我去试试看。[Quote=引用 23 楼 acmain_chm 的回复:]
SQL codedelete afrom tb a,(select stepfrom tbwhere ID=431) bwhere instr(a.Step,b.Step)=1 ;

SQL codemysql>select*from tb;+--------+--------+-------------+| id| step| name|+--------+--------+-------------+|431|100| 安徽省||53425|100001| 安徽省县城1||412123|100002| 安徽省县城2||54325|100003| 安徽省县城3||54325|101| 江苏省||41234|101001| 江苏省县城1||5654|101002| 江苏省县城2||8687|101003| 江苏省县城3|+--------+--------+-------------+8 rowsinset (0.00 sec)

mysql>delete afrom tb a,(select stepfrom tbwhere ID=431) bwhere instr(a.Step,b.Step)=1 ;
Query OK,4 rows affected (0.06 sec)

mysql>select*from tb;+-------+--------+-------------+| id| step| name|+-------+--------+-------------+|54325|101| 江苏省||41234|101001| 江苏省县城1||5654|101002| 江苏省县城2||8687|101003| 江苏省县城3|+-------+--------+-------------+4 rowsinset (0.00 sec)

mysql>
[/Quote]
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
因为ID是自动生成的,没有规律可循,所以我通过STEP删除,但是STEP得不到,只能得到ID,于是通过就产了了问题咯, 通过得到ID来查处100这个STEP,然后通过这个100,来删除100以及包括它下面的100001,100002,100003. 我做的东西是办公自动化的数据库。里面有部门。部门有上下级之分。。当我想要废弃一个部门的时候,本来是可以直接通过ID删除这个部门的。但是他还有子部门,也就是STEP增三位的子部门,于是我还要删除掉这些子部门。希望能用一个删除语句搞定。。。

不好意思,语言表述能力垃圾,书到用时方恨少。。。[Quote=引用 20 楼 acmain_chm 的回复:]
少了个分号。

你是不是想输入431 ,然后删除所有的
mysql> select * from tb;
+--------+--------+-------------+
| id    | step  | name        |
+--------+--------+-------------+
| 431    | 100    | 安徽省      |
| 53425  | 100001 | 安徽省县城1 |
| 412123 | 100002 | 安徽省县城2 |
| 54325  | 100003 | 安徽省县城3 |
| 54325  | 101    | 江苏省      |
| 41234  | 101001 | 江苏省县城1 |
| 5654  | 101002 | 江苏省县城2 |
| 8687  | 101003 | 江苏省县城3 |
+--------+--------+-------------+
8 rows in set (0.00 sec)

mysql>
[/Quote]
ACMAIN_CHM 2010-01-07
  • 打赏
  • 举报
回复
mysql> delete a from tb a,(select step from tb where ID=5654) b where instr(a.Step,b.Step)=1 ;
Query OK, 1 row affected (0.06 sec)

mysql> select * from tb;
+-------+--------+-------------+
| id | step | name |
+-------+--------+-------------+
| 54325 | 101 | 江苏省 |
| 41234 | 101001 | 江苏省县城1 |
| 8687 | 101003 | 江苏省县城3 |
+-------+--------+-------------+
3 rows in set (0.00 sec)

mysql>
ACMAIN_CHM 2010-01-07
  • 打赏
  • 举报
回复
delete a from tb a,(select step from tb where ID=431) b where instr(a.Step,b.Step)=1 ;


mysql> select * from tb;
+--------+--------+-------------+
| id | step | name |
+--------+--------+-------------+
| 431 | 100 | 安徽省 |
| 53425 | 100001 | 安徽省县城1 |
| 412123 | 100002 | 安徽省县城2 |
| 54325 | 100003 | 安徽省县城3 |
| 54325 | 101 | 江苏省 |
| 41234 | 101001 | 江苏省县城1 |
| 5654 | 101002 | 江苏省县城2 |
| 8687 | 101003 | 江苏省县城3 |
+--------+--------+-------------+
8 rows in set (0.00 sec)

mysql> delete a from tb a,(select step from tb where ID=431) b where instr(a.Step,b.Step)=1 ;
Query OK, 4 rows affected (0.06 sec)

mysql> select * from tb;
+-------+--------+-------------+
| id | step | name |
+-------+--------+-------------+
| 54325 | 101 | 江苏省 |
| 41234 | 101001 | 江苏省县城1 |
| 5654 | 101002 | 江苏省县城2 |
| 8687 | 101003 | 江苏省县城3 |
+-------+--------+-------------+
4 rows in set (0.00 sec)

mysql>
wwwwb 2010-01-07
  • 打赏
  • 举报
回复
MYSQL不支持
delete from tab where step like concat((select Step from tab where id='123123'),'%')
这种写法,其它数据库支持
wwwwb 2010-01-07
  • 打赏
  • 举报
回复
DELETE a FROM tbd a INNER JOIN tbd b
ON INSTR(a.name,b.name)>0
WHERE b.step=100
ACMAIN_CHM 2010-01-07
  • 打赏
  • 举报
回复
少了个分号。

你是不是想输入431 ,然后删除所有的
mysql> select * from tb;
+--------+--------+-------------+
| id | step | name |
+--------+--------+-------------+
| 431 | 100 | 安徽省 |
| 53425 | 100001 | 安徽省县城1 |
| 412123 | 100002 | 安徽省县城2 |
| 54325 | 100003 | 安徽省县城3 |

| 54325 | 101 | 江苏省 |
| 41234 | 101001 | 江苏省县城1 |
| 5654 | 101002 | 江苏省县城2 |
| 8687 | 101003 | 江苏省县城3 |
+--------+--------+-------------+
8 rows in set (0.00 sec)

mysql>
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
麻烦您这么长时间实在不好意思。。。这个问题我问主管他也没搞定。。不知道什么原因[Quote=引用 18 楼 acmain_chm 的回复:]
insert into tb values(5654 , 101002  '江苏省县城2')

语法错误,不会吧。建议提供前先自己试一下!
[/Quote]
ACMAIN_CHM 2010-01-07
  • 打赏
  • 举报
回复
insert into tb values(5654 , 101002 '江苏省县城2')

语法错误,不会吧。建议提供前先自己试一下!
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
里面的SELECT语句能得到STEP的值,整句也能执行,但是删除不了。。。[Quote=引用 12 楼 wwwwb 的回复:]
引用 9 楼 mutoujuelian 的回复:
晕,ID不是等于100001啊 麻烦你再帮我看下4楼我后面加上去的引用 7 楼 wwwwb 的回复:
delete from  tab where step like concat((select id from tab where id='100001'),'%')



delete from  tab where step like concat((select Step from tab where id='123123'),'%')
[/Quote]
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
抄错了,,ID和STEP是 int(10)[Quote=引用 14 楼 acmain_chm 的回复:]
  1. 你的 create table xxx .. 语句
  2. 你的 insert into xxx ... 语句

提供这两样。我连你的ID/STEP字段是数字还是字符都不知道。
[/Quote]
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
建表如下:    求DELETE语句
create table tb
(
id varchar(10),
step varchar(10),
name varchar(20)
)
insert into tb values(431 , 100,'安徽省')
insert into tb values(53425 , 100001 '安徽省县城1')
insert into tb values(412123 , 100002 '安徽省县城2')
insert into tb values(54325 , 100003 '安徽省县城3')
insert into tb values(54325 , 101 '江苏省')
insert into tb values(41234 , 101001 '江苏省县城1')
insert into tb values(5654 , 101002 '江苏省县城2')
insert into tb values(8687 , 101003 '江苏省县城3')

ID是主键,自动生成的。我能得到ID的值,但是得不到Step的值,所以我要通过ID来得到Step的值,
因为我删除的要求是 当我删除Step=100的这一行,那么它的下属单位100001,100002,100003也会被删除。那么这就可能用到 DELETE FROM tb where step LIKE '100%'


我需要得到类似这样的语句

delete from tb where STEP=? 是要带问号的语句

我写的是这样的,但是是错误的
<DeleteCommands>
<SQL>
<CommandText>delete from G_Menu where Step like concat((select Step from g_menu where id = ?ID),'%')</CommandText>
<ParameterCollection>
<Parameter Name="ID" Type="Long"/>
</ParameterCollection>
</SQL>
</DeleteCommands>
ACMAIN_CHM 2010-01-07
  • 打赏
  • 举报
回复
  1. 你的 create table xxx .. 语句
  2. 你的 insert into xxx ... 语句


提供这两样。我连你的ID/STEP字段是数字还是字符都不知道。
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
啊? 我用的是MySQL 5.1.。。这版不是MySQL么?[Quote=引用 11 楼 acmain_chm 的回复:]
(不要高估你的汉语表达能力或者我的汉语理解能力)
  建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
  参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
 
  1. 你的 create table xxx .. 语句
  2. 你的 insert into xxx ... 语句
  3. 结果是什么样,(并给以简单的算法描述)
  4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
 
  这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

 
[/Quote]
wwwwb 2010-01-07
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 mutoujuelian 的回复:]
晕,ID不是等于100001啊 麻烦你再帮我看下4楼我后面加上去的引用 7 楼 wwwwb 的回复:
delete from  tab where step like concat((select id from tab where id='100001'),'%')

[/Quote]

delete from tab where step like concat((select Step from tab where id='123123'),'%')
ACMAIN_CHM 2010-01-07
  • 打赏
  • 举报
回复
(不要高估你的汉语表达能力或者我的汉语理解能力)
建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html

1. 你的 create table xxx .. 语句
2. 你的 insert into xxx ... 语句
3. 结果是什么样,(并给以简单的算法描述)
4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)

这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。

mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
还是不行呀。。。[Quote=引用 5 楼 mutoujuelian 的回复:]
我不是要这样的结果呀。。。麻烦你再帮看下引用 2 楼 acmain_chm 的回复:
delete from A where left(ID,7)='100001'

[/Quote]
mutoujuelian 2010-01-07
  • 打赏
  • 举报
回复
晕,ID不是等于100001啊 麻烦你再帮我看下4楼我后面加上去的[Quote=引用 7 楼 wwwwb 的回复:]
delete from  tab where step like concat((select id from tab where id='100001'),'%')
[/Quote]
wwwwb 2010-01-07
  • 打赏
  • 举报
回复
delete from tab where step like concat((select Step from tab where id='123123'),'%')
加载更多回复(7)

56,678

社区成员

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

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