delete using where and or

前端 未结 4 451
自闭症患者
自闭症患者 2020-12-04 01:11

I have a database table [id,first,second,third] with a lot of entries and I would like to delete all the entries when [first,second,third] are empt

相关标签:
4条回答
  • 2020-12-04 01:56

    Remove the star

    $sql= "delete from mytable where first='' or second='' or third=''";
    

    You don't need that with the delete statement

    0 讨论(0)
  • 2020-12-04 02:07

    It's delete from, not delete * from

    0 讨论(0)
  • 2020-12-04 02:08

    Adding something new to what hasn't already been said:

    The asterisk isn't something that DELETE can use, only SELECT can. Yet what I am adding here is that an (mysql) aggregate function such as COUNT() can also use the asterisk.

    An basic example:

    SELECT COUNT(*) AS Total
    FROM products
    

    Here are their respective references:

    • https://dev.mysql.com/doc/refman/8.0/en/delete.html
    • https://dev.mysql.com/doc/refman/8.0/en/select.html
    • https://dev.mysql.com/doc/refman/8.0/en/counting-rows.html

    Notes:

    Some coders who are new to working with databases who used a SELECT query for something that worked for them, might have thought that using the asterisk (a.k.a. "star") in a DELETE statement uses the same syntax as SELECT and that it would delete everything. It's logical though, but it doesn't quite work that way with DELETE.

    What needs to be used would either be TRUNCATE or DROP TABLE depending on what you want to do exactly. Delete just the selected records, all of the records or the table itself? The decision is yours.

    For a specific record, use the WHERE clause.

    Warning

    Be careful with DROP TABLE, it will delete everything including any existing columns empty or not and their definitions.

    Use TRUNCATE to only delete all of the records and not the columns and their definitions.

    Please consult the manuals before usage:

    • https://dev.mysql.com/doc/refman/8.0/en/truncate-table.html
    • https://dev.mysql.com/doc/refman/8.0/en/drop-table.html

    Footnote:

    For those (re)visiting the question, please note that the mysql_* api is deprecated and deleted (no longer supported) in PHP 7.0.

    Upgrade to either the mysqli_* or PDO api.

    Consult the following references:

    • http://php.net/manual/en/migration55.deprecated.php
    • http://php.net/manual/en/book.mysqli.php
    • http://php.net/manual/en/book.pdo.php
    0 讨论(0)
  • 2020-12-04 02:10

    You don't need * in this statement.

    $sql= "delete from mytable where first='' or second='' or third=''";
    
    0 讨论(0)
提交回复
热议问题