mysql查找并删除记录_mysql 子查询删除记录

mysql 子查询删除记录

mysql 可以通过子查询得到要删除的记录条件,然后通过子查询得结果匹配要删除的记录。但是 mysql 不能直接删除子查询表中的数据,必须通过一个临时表来解决。例如:

delete from t_transaction where exists

(select d.* from t_ti_category a,t_category b,t_trade_item c,t_transaction d

where b.FID=a.FCategory and a.FTradeItem=c.FID and c.FTrans=d.FID and b.FID=2549 and              t_transaction.FID=d.FID)

在该 sql 语句中由于子查询中包含 t_transaction 表,但我们同时要删除 t_transaction 表中的记录,所以允许该语句时报如下错误:

1093-You can't specify target table 't_transaction' for update in from clause.

修改后的操作语句如下:

delete from t_transaction where exists

(select e.FID from

(select d.* from t_ti_category a,t_category b,t_trade_item c,t_transaction d

where b.FID=a.FCategory and a.FTradeItem=c.FID and c.FTrans=d.FID and b.FID=2549) e

where t_transaction.FID=e.FID)

相关文档:

在Ubuntu下安装的MySQL默认配置下的数据库文件存储位置是在/var/lib/mysql,

MySQL数据库的文件结构是怎么样的呢,对于这样子创建的数据库命令:

CREATE DATABASE mysqldb

在默认的数据库存储位置下就会有个文件夹mysqldb。要操作数据库首先得停止数据库进程:

$sudo /etc/init.d/mysql stop

本文以转 ......

连接:mysql -h主机地址 -u用户名 -p用户密码 (注:u与root可以不用加空格,其它也一样)

断开:exit (回车)

创建授权:grant select on 数据库.* to 用户名@登录主机 identified by \"密码\"

修改密码:mysqladmin -u用户名 -p旧密码 password 新密码

删除授权: revoke select,insert,update,delete om *.* from ......

# MySQL Server Instance Configuration File

# ----------------------------------------------------------------------

# Generated by the MySQL Server Instance Configuration Wizard

#

#

# Installation Instructions

# ----------------------------------------------------------------------

#

# On Li ......

/*查询数据库*/

show databases;

/*创建数据库*/

create database dengheping;

/*使用数据库*/

use dengheping;

/*创建表*/

create table user(id int primary key auto_increment,name char(255));

/*查询表*/

show tables;

/*显示表结构*/

desc user;

/*插入字段*/

alter table user add password varchar(6 ......

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值