Mysql中如何删除空白行?

发布于 2024-10-11 10:28:11 字数 85 浏览 4 评论 0原文

我确实有一个包含超过 100000 个数据元素的表,但其中有近 350 个空白行。如何使用 phpmyadmin 删除这些空白行?手动删除是一项繁琐的任务。

I do have a table with more than 100000 data elements, but there are almost 350 blank rows within. How do I delete this blank rows using phpmyadmin? Manually deleting is a tedious task.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

丑疤怪 2024-10-18 10:28:12

我正在 Windows 的命令提示符下执行 mysql 操作。基本查询:

delete * from table_name where column=''

并且

delete * from table_name where column='NULL'

不起作用。我不知道它是否在 phpmyadmin sqlcommand builder 中工作。无论如何:

delete * from table_name where column is NULL 

工作正常。

I am doing the mysql operation in command prompt in windows. And the basic queries:

delete * from table_name where column=''

and

delete * from table_name where column='NULL'

doesn't work. I don't know whether it works in phpmyadmin sqlcommand builder. Anyway:

delete * from table_name where column is NULL 

works fine.

冷情妓 2024-10-18 10:28:12

我有一个 PHP 脚本,可以根据列数据类型自动删除空行。

这使我能够针对不同的列类型以不同的方式定义“空”。

例如,

table
first_name (varchar) | last_name (varchar) | some_qty ( int ) | other_qty (decimal)

DELETE FROM `table` WHERE
(`first_name` IS NULL OR `first_name` = '')
AND
(`last_name` IS NULL OR `last_name` = '')
AND
(`some_qty` IS NULL OR `some_qty` = 0)
AND
(`other_qty` IS NULL OR `other_qty` = 0)

由于“0”值在我的系统中没有意义,因此我将它们视为空。但我发现如果你这样做 (first_name = 0) 那么你总是会得到 true,因为在 MySQL 中字符串总是 == 0。所以我根据数据类型定制了“空”的定义。

I have a PHP script that automatically removes empty rows based on column data types.

That allows me to define "emptiness" differently for different column types.

e.g.

table
first_name (varchar) | last_name (varchar) | some_qty ( int ) | other_qty (decimal)

DELETE FROM `table` WHERE
(`first_name` IS NULL OR `first_name` = '')
AND
(`last_name` IS NULL OR `last_name` = '')
AND
(`some_qty` IS NULL OR `some_qty` = 0)
AND
(`other_qty` IS NULL OR `other_qty` = 0)

Since "0" values are meaningless in my system, I count them as empty. But I found out that if you do (first_name = 0) then you will always get true, because strings always == 0 in MySQL. So I tailor the definition of "empty" to the data type.

不羁少年 2024-10-18 10:28:12

此过程将删除所有为空的列的任何行,忽略可能设置为 ID 的主列。我希望它对你有帮助。

DELIMITER //
CREATE PROCEDURE DeleteRowsAllColNull(IN tbl VARCHAR(64))
BEGIN
SET @tbl = tbl;
SET SESSION group_concat_max_len = 1000000;
SELECT CONCAT('DELETE FROM `',@tbl,'` WHERE ',(REPLACE(group_concat(concat('`',COLUMN_NAME, '` is NULL')),',',' AND ')),';') FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = @tbl AND COLUMN_KEY NOT LIKE 'PRI' into @delete_all;
PREPARE delete_all FROM @delete_all;
EXECUTE delete_all;
DEALLOCATE PREPARE delete_all;
END //
DELIMITER ;

像这样执行程序。

CALL DeleteRowsAllColNull('your table');

This procedure will delete any row for all columns that are null ignoring the primary column that may be set as an ID. I hope it helps you.

DELIMITER //
CREATE PROCEDURE DeleteRowsAllColNull(IN tbl VARCHAR(64))
BEGIN
SET @tbl = tbl;
SET SESSION group_concat_max_len = 1000000;
SELECT CONCAT('DELETE FROM `',@tbl,'` WHERE ',(REPLACE(group_concat(concat('`',COLUMN_NAME, '` is NULL')),',',' AND ')),';') FROM INFORMATION_SCHEMA.COLUMNS WHERE table_name = @tbl AND COLUMN_KEY NOT LIKE 'PRI' into @delete_all;
PREPARE delete_all FROM @delete_all;
EXECUTE delete_all;
DEALLOCATE PREPARE delete_all;
END //
DELIMITER ;

Execute the procedure like this.

CALL DeleteRowsAllColNull('your table');
我不是你的备胎 2024-10-18 10:28:12

我知道这个问题已经得到回答并且已经被勾选,但我为此编写了一个小函数,并认为它可能对其他人有用。

我使用数组调用函数,以便可以对不同的表使用相同的函数。

$tableArray=array("Address", "Email", "Phone"); //This is the column names
$this->deleteBlankLines("tableName",$tableArray);

这是接受数组并构建删除字符串的函数

private function deleteBlankLines($tablename,$columnArray){
    $Where="";
    foreach($columnArray as $line):
        $Where.="(`".$line."`=''||`".$line."` IS NULL) && ";
    endforeach;
    $Where = rtrim($Where, '&& ');  
    $query="DELETE FROM `{$tablename}` WHERE ".$Where;
    $stmt = $this->db->prepare($query);
    $stmt->execute();
}

您可以将此函数用于多个表。您只需发送不同的表名称和数组即可工作。

我的函数将同时检查整行的空列或 NULL 列。如果您不需要它来检查 NULL,那么您可以删除该部分。

I know this has already been answered and has got a tick, but I wrote a small function for doing this, and thought it might be useful to other people.

I call my function with an array so that I can use the same function for different tables.

$tableArray=array("Address", "Email", "Phone"); //This is the column names
$this->deleteBlankLines("tableName",$tableArray);

and here is the function which takes the array and builds the delete string

private function deleteBlankLines($tablename,$columnArray){
    $Where="";
    foreach($columnArray as $line):
        $Where.="(`".$line."`=''||`".$line."` IS NULL) && ";
    endforeach;
    $Where = rtrim($Where, '&& ');  
    $query="DELETE FROM `{$tablename}` WHERE ".$Where;
    $stmt = $this->db->prepare($query);
    $stmt->execute();
}

You can use this function for multiple tables. You just need to send in a different table name and array and it will work.

My function will check for a whole row of empty columns or NULL columns at the same time. If you don't need it to check for NULL then you can remove that part.

非要怀念 2024-10-18 10:28:11

一般答案是:

DELETE FROM table_name WHERE some_column = '';

DELETE FROM table_name WHERE some_column IS NULL;

参见:http://dev.mysql.com /doc/refman/5.0/en/delete.html

发布表格时会提供更多信息!~

另外,请务必执行以下操作:

SELECT * FROM table_name WHERE some_column = '';

在删除之前,以便您可以看到要删除的行!我认为在 phpMyAdmin 中您甚至可以只进行选择,然后“全选”并删除,但我不确定。这会非常快,而且非常安全。

The general answer is:

DELETE FROM table_name WHERE some_column = '';

or

DELETE FROM table_name WHERE some_column IS NULL;

See: http://dev.mysql.com/doc/refman/5.0/en/delete.html

More info when you post your tables!~

Also, be sure to do:

SELECT * FROM table_name WHERE some_column = '';

before you delete, so you can see which rows you are deleting! I think in phpMyAdmin you can even just do the select and then "select all" and delete, but I'm not sure. This would be pretty fast, and very safe.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文