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 ......