php \ mysql在一个查询中从两个不相关的表中删除数据

时间:2016-06-19 22:28:41

标签: php mysql

我必须表tbl1和tbl2并且它们之间没有关系,我想使用一个查询从两个表中删除不同的数据是可能的,或者我应该在两个单独的查询中执行它

我尝试了以下查询

#include <iostream>

using namespace std;

int main() {

int count, x, i = 0;
int nums[10][10000] = { 0 };

for (count = 0; count < 10000; count++) {
    x = rand() % 100;
    if (x >= 0 && x <= 9)
    {
        nums[0][1]++;
    }
    else if (x >= 10 && x <= 19)
    {
        nums[1][1]++;
    }
    else if (x >= 20 && x <= 29)
    {
        nums[2][1]++;
    }
    else if (x >= 30 && x <= 39)
    {
        nums[3][1]++;
    }
    else if (x >= 40 && x <= 49)
    {
        nums[4][1]++;
    }
    else if (x >= 50 && x <= 59)
    {
        nums[5][1]++;
    }
    else if (x >= 60 && x <= 69)
    {
        nums[6][1]++;
    }
    else if (x >= 70 && x <= 79)
    {
        nums[7][1]++;
    }
    else if (x >= 80 && x <= 89)
    {
        nums[8][1]++;
    }
    else
    {
        nums[9][1]++;
    }
}

    cout << "0-9: ";
    for (i = 0; i < nums[0][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "10-19: ";
    for (i = 0; i < nums[1][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "20-29: ";
    for (i = 0; i < nums[2][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "30-39: ";
    for (i = 0; i < nums[3][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "40-49: ";
    for (i = 0; i < nums[4][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "50-59: ";
    for (i = 0; i < nums[5][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "60-69: ";
    for (i = 0; i < nums[6][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "70-79: ";
    for (i = 0; i < nums[7][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "80-89: ";
    for (i = 0; i < nums[8][1]; i++) { cout << "X"; }
    cout << endl;

    cout << "90-99: ";
    for (i = 0; i < nums[9][1]; i++) { cout << "X"; }
    cout << endl;

}

但是我收到以下错误: 您的SQL语法有错误;查看与您的MariaDB服务器版本对应的手册,以便在#ttl1.llete_time附近使用正确的语法IS NOT null和tbl1.delete_time!=&#39; 0000-0&#39;在第1行

提前致谢

1 个答案:

答案 0 :(得分:-1)

通过将查询与;分开来改变您的查询,如下所示

DELETE FROM tbl1 WHERE delete_time IS NOT null AND delete_time != '0000-00-00 00:00:00'; DELETE FROM tbl2 WHERE delete_time IS NOT NULL AND delete_time != '0000-00-00 00:00:00'; 

(或)推荐方式;将DELETE语句包装在存储过程中,如

create procedure usp_deleteData
as
begin
    DELETE FROM tbl1 
    WHERE delete_time IS NOT null 
    AND delete_time != '0000-00-00 00:00:00'; 

    DELETE FROM tbl2 
    WHERE delete_time IS NOT NULL 
    AND delete_time != '0000-00-00 00:00:00'; 
end

此外,虽然表之间没有任何关系,但您有一个名为delete_time的公共列,因此您可以执行Multi Table DELETE-JOIN之类的

DELETE tbl1, tbl2 FROM tbl1 
INNER JOIN tbl2 
WHERE tbl2.delete_time = tbl1.delete_time
AND tbl2.delete_time IS NOT NULL
AND tbl1.delete_time IS NOT NULL
AND '0000-00-00 00:00:00' NOT IN (tbl1.delete_time, tbl2.delete_time);