从多个数据库中的一个表中删除行?

时间:2013-05-31 21:03:29

标签: mysql

我有多个数据库,每个数据库都有一些共同的表。我想做DELETE FROM *.table_name WHERE col_name LIKE 'value';后跟OPTIMIZE TABLE *.table_name;

之类的事情

这可以通过单个MYSQL查询实现,还是需要在每个数据库中单独运行这些查询?

1 个答案:

答案 0 :(得分:4)

如果您有三个包含表mytable的数据库,可能会这样做

DELETE A.*,B.*,C.*
FROM db1.mytable A,db2.mytable B,db3.mytable C
WHERE A.col_name LIKE 'value'
AND   B.col_name LIKE 'value'
AND   C.col_name LIKE 'value';
OPTIMIZE TABLE db1.mytable,db2.mytable,db3.mytable;

试一试!!!

更新2013-05-31 18:27 EDT

我有一个基于INFORMATION_SCHEMA数据库和动态SQL的解决方案。

USE db1
SET @GivenTable = 'mytb';
SET @GivenCol   = 'dat';
SET @GivenValue = 'Burger King';
SET @x = 0;
SELECT GROUP_CONCAT(dbtb) INTO @OptimizeTableList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
FROM information_schema.columns WHERE table_name=@GivenTable
and column_name=@GivenCol) AAA) AA;
SET @x = 0;
SELECT GROUP_CONCAT(dbalias,'.*') INTO @DeleteTableList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
FROM information_schema.columns WHERE table_name=@GivenTable
and column_name=@GivenCol) AAA) AA;
SET @x = 0;
SELECT GROUP_CONCAT(dbtb,' AS ',dbalias) INTO @FromList
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
FROM information_schema.columns WHERE table_name=@GivenTable
and column_name=@GivenCol) AAA) AA;
SET @x = 0;
SELECT GROUP_CONCAT(CONCAT(dbalias,'.',col,'=',
QUOTE(@GivenValue)) SEPARATOR ' AND ') INTO @WhereClause
FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias,col
FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
FROM information_schema.columns WHERE table_name=@GivenTable
and column_name=@GivenCol) AAA) AA;
SET @OptTableSQL = CONCAT('OPTIMIZE TABLE ',@OptimizeTableList);
SET @DeleteSQL = CONCAT('DELETE ',@DeleteTableList,' FROM ',@FromList,' WHERE ',@WhereClause);
PREPARE st FROM @DeleteSQL;   EXECUTE st; DEALLOCATE PREPARE st;
PREPARE st FROM @OptTableSQL; EXECUTE st; DEALLOCATE PREPARE st;
SELECT * FROM db1.mytb;
SELECT * FROM db2.mytb;
SELECT * FROM db3.mytb;

样本数据

DROP DATABASE IF EXISTS db1;
DROP DATABASE IF EXISTS db2;
DROP DATABASE IF EXISTS db3;
CREATE DATABASE db1;
CREATE DATABASE db2;
CREATE DATABASE db3;
CREATE TABLE db1.mytb
(id int not null auto_increment,
dat varchar(20),
primary key (id),key (dat));
INSERT INTO db1.mytb (dat) VALUES
('McDonald''s'),('Wendy''s'),
('Burger King'),('Jack in the Box');
CREATE TABLE db2.mytb LIKE db1.mytb;
CREATE TABLE db3.mytb LIKE db1.mytb;
INSERT INTO db2.mytb (dat) SELECT dat FROM db1.mytb;
INSERT INTO db3.mytb (dat) SELECT dat FROM db1.mytb;
SELECT * FROM db1.mytb;
SELECT * FROM db2.mytb;
SELECT * FROM db3.mytb;

加载示例数据

mysql> DROP DATABASE IF EXISTS db1;
Query OK, 1 row affected (0.00 sec)

mysql> DROP DATABASE IF EXISTS db2;
Query OK, 1 row affected (0.00 sec)

mysql> DROP DATABASE IF EXISTS db3;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE DATABASE db1;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE DATABASE db2;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE DATABASE db3;
Query OK, 1 row affected (0.00 sec)

mysql> CREATE TABLE db1.mytb
    -> (id int not null auto_increment,
    -> dat varchar(20),
    -> primary key (id),key (dat));
Query OK, 0 rows affected (0.08 sec)

mysql> INSERT INTO db1.mytb (dat) VALUES
    -> ('McDonald''s'),('Wendy''s'),
    -> ('Burger King'),('Jack in the Box');
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> CREATE TABLE db2.mytb LIKE db1.mytb;
Query OK, 0 rows affected (0.06 sec)

mysql> CREATE TABLE db3.mytb LIKE db1.mytb;
Query OK, 0 rows affected (0.03 sec)

mysql> INSERT INTO db2.mytb (dat) SELECT dat FROM db1.mytb;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> INSERT INTO db3.mytb (dat) SELECT dat FROM db1.mytb;
Query OK, 4 rows affected (0.00 sec)
Records: 4  Duplicates: 0  Warnings: 0

mysql> SELECT * FROM db1.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  1 | McDonald's      |
|  2 | Wendy's         |
|  3 | Burger King     |
|  4 | Jack in the Box |
+----+-----------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM db2.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  1 | Burger King     |
|  2 | Jack in the Box |
|  3 | McDonald's      |
|  4 | Wendy's         |
+----+-----------------+
4 rows in set (0.00 sec)

mysql> SELECT * FROM db3.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  1 | Burger King     |
|  2 | Jack in the Box |
|  3 | McDonald's      |
|  4 | Wendy's         |
+----+-----------------+
4 rows in set (0.00 sec)

mysql>

解决方案执行

mysql> USE db1
Database changed
mysql> SET @GivenTable = 'mytb';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @GivenCol   = 'dat';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @GivenValue = 'Burger King';
Query OK, 0 rows affected (0.00 sec)

mysql> SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT GROUP_CONCAT(dbtb) INTO @OptimizeTableList
    -> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
    -> FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
    -> FROM information_schema.columns WHERE table_name=@GivenTable
    -> and column_name=@GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)

mysql> SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT GROUP_CONCAT(dbalias,'.*') INTO @DeleteTableList
    -> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
    -> FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
    -> FROM information_schema.columns WHERE table_name=@GivenTable
    -> and column_name=@GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)

mysql> SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT GROUP_CONCAT(dbtb,' AS ',dbalias) INTO @FromList
    -> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias
    -> FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
    -> FROM information_schema.columns WHERE table_name=@GivenTable
    -> and column_name=@GivenCol) AAA) AA;
Query OK, 1 row affected (0.02 sec)

mysql> SET @x = 0;
Query OK, 0 rows affected (0.00 sec)

mysql> SELECT GROUP_CONCAT(CONCAT(dbalias,'.',col,'=',
    -> QUOTE(@GivenValue)) SEPARATOR ' AND ') INTO @WhereClause
    -> FROM (SELECT CONCAT(db,'.',tb) dbtb,CONCAT('dbalias',num) dbalias,col
    -> FROM (SELECT table_schema db,table_name tb,(@x:=@X+1) num,column_name col
    -> FROM information_schema.columns WHERE table_name=@GivenTable
    -> and column_name=@GivenCol) AAA) AA;
Query OK, 1 row affected (0.03 sec)

mysql> SET @OptTableSQL = CONCAT('OPTIMIZE TABLE ',@OptimizeTableList);
Query OK, 0 rows affected (0.00 sec)

mysql> SET @DeleteSQL = CONCAT('DELETE ',@DeleteTableList,' FROM ',@FromList,' WHERE ',@WhereClause);
Query OK, 0 rows affected (0.00 sec)

mysql> PREPARE st FROM @DeleteSQL;   EXECUTE st; DEALLOCATE PREPARE st;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

Query OK, 3 rows affected (0.00 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> PREPARE st FROM @OptTableSQL; EXECUTE st; DEALLOCATE PREPARE st;
Query OK, 0 rows affected (0.00 sec)
Statement prepared

+----------+----------+----------+----------+
| Table    | Op       | Msg_type | Msg_text |
+----------+----------+----------+----------+
| db1.mytb | optimize | status   | OK       |
| db2.mytb | optimize | status   | OK       |
| db3.mytb | optimize | status   | OK       |
+----------+----------+----------+----------+
3 rows in set (0.05 sec)

Query OK, 0 rows affected (0.00 sec)

mysql> SELECT * FROM db1.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  1 | McDonald's      |
|  2 | Wendy's         |
|  4 | Jack in the Box |
+----+-----------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM db2.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  2 | Jack in the Box |
|  3 | McDonald's      |
|  4 | Wendy's         |
+----+-----------------+
3 rows in set (0.01 sec)

mysql> SELECT * FROM db3.mytb;
+----+-----------------+
| id | dat             |
+----+-----------------+
|  2 | Jack in the Box |
|  3 | McDonald's      |
|  4 | Wendy's         |
+----+-----------------+
3 rows in set (0.01 sec)

mysql>

查询的内容是什么

mysql> SELECT @OptimizeTableList,@DeleteTableList,@FromList,
    -> @WhereClause,@OptTableSQL,@DeleteSQL\G
*************************** 1. row ***************************
@OptimizeTableList: db1.mytb,db2.mytb,db3.mytb
  @DeleteTableList: dbalias1.*,dbalias2.*,dbalias3.*
         @FromList: db1.mytb AS dbalias1,db2.mytb AS dbalias2,db3.mytb AS dbalias3
      @WhereClause: dbalias1.dat='Burger King' AND dbalias2.dat='Burger King' AND dbalias3.dat='Burger King'
      @OptTableSQL: OPTIMIZE TABLE db1.mytb,db2.mytb,db3.mytb
        @DeleteSQL: DELETE dbalias1.*,dbalias2.*,dbalias3.* FROM db1.mytb AS dbalias1,db2.mytb AS dbalias2,db3.mytb AS dbalias3 WHERE dbalias1.dat='Burger King' AND dbalias2.dat='Burger King' AND dbalias3.dat='Burger King'
1 row in set (0.00 sec)

mysql>

CAVEAT

你必须站在三个数据库中的一个才能工作

你有意思吗?嘿,我也是!

有关多表DELETE的详细信息,请参阅MySQL Documentation。搜索短语multiple-table DELETE statement