Home > php教程 > php手册 > mysql delete multi-table data and delete associated data php multi-table deletion

mysql delete multi-table data and delete associated data php multi-table deletion

WBOY
Release: 2016-08-26 10:12:49
Original
2061 people have browsed it

The user integrates it into PHP by himself. Use PHP’s execute function. Do not use query
delete in MYSQL to delete multi-table data and delete related data
There are many ways to delete data in MySQL. The most commonly used method is to use delete to delete records. Below I will introduce some simple examples of delete to delete a single record and delete multi-table associated data.
1. delete from t1 where condition
2. delete t1 from t1 where condition
3. delete t1 from t1,t2 where condition
4. delete t1,t2 from t1,t2 where condition
The first 3 are feasible, but the 4th is not.
That is to say, simply using the delete statement cannot delete data from multiple tables, but you can create a cascade delete and establish a cascade between two tables
By deleting a relationship, when you delete data in one table, you can delete related data in another table at the same time.
1. Delete all records from data table t1 that have matching ID values ​​in data table t2
The code is as follows
DELETE t1 FROM t1,t2 WHERE t1.id=t2.id or DELETE FROM t1 USING t1,t2 WHERE t1.id=t2.id
2. Find and delete records from data table t1 that do not match in data table t2
The code is as follows
DELETE t1 FROM t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL or
DELETE FROM t1,USING t1 LEFT JOIN T2 ON t1.id=t2.id WHERE t2.id IS NULL
3. Find the same recorded data from the two tables and delete the data in both tables
The code is as follows
DELETE t1,t2 from t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t1.id=25
Note that t1 and t2 in delete t1 and t2 from here cannot be aliases
Such as:
The code is as follows
delete t1,t2 from table_name as t1 left join table2_name as t2 on t1.id=t2.id where
table_name.id=25
It is wrong to execute in the data (MYSQL version is not less than 5.0, it is ok in 5.0)
The above sentence is rewritten as
The code is as follows
delete table_name,table2_name from table_name as t1 left join table2_name as t2 on
t1.id=t2.id where table_name.id=25
It is wrong to execute in the data (MYSQL version is less than 5.0, but it is OK in 5.0
)
How DELETE deletes associated data and implements cascade deletion
create TABLE IF NOT EXISTS `dcsmember`(
`id` int(3) auto_increment not null primary key,
`name` varchar(12) not null,
`password` varchar(40) not null,
`phonenumber` char(20),
`time` DATETIME NOT NULL,
`jifen` int(8) not null DEFAULT '20',
`email` varchar(40) NOT NULL,
`power` int(2) NOT NULL DEFAULT '1'
)CHARACTER SET gb2312";
create TABLE IF NOT EXISTS `sp`(
`spid` int(5) auto_increment not null primary key,
`spuserid` int(3) not null,
`spname` varchar(12) not null,
`spmoney` float(6) not null,
`spopt` char(20) not null,
`spsay` varchar(50),
`sptime` DATE DEFAULT '2008-10-01',
`spendor` TINYINT(1) DEFAULT '1',
INDEX ( `spuserid` )
)CHARACTER SET gb2312";

The code is as follows

$sqldel="DELETE FROM dcsmember WHERE email='$value'";
Now I use to delete the member’s information from the member table
, you also need to delete the member's information in another table. dcsmember.ID is a foreign code, corresponding to sp.spuserid,?
How to write it
The code is as follows
delete d, s from dcsmember d inner join sp s on d.id = s.spuserid where d.email="xxxxxx"

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template