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
Remove the star
$sql= "delete from mytable where first='' or second='' or third=''";
You don't need that with the delete
statement
It's delete from
, not delete * from
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:
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.
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:
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:
You don't need *
in this statement.
$sql= "delete from mytable where first='' or second='' or third=''";