Fix ERROR 1093 (HY000): You can't specify target table for update in FROM clause while deleting the lowest value from a MySQL column?



Let us first create a table −

mysql> create table DemoTable1597
   -> (
   -> Marks int
   -> );
Query OK, 0 rows affected (0.69 sec)

Insert some records in the table using insert command −

mysql> insert into DemoTable1597 values(45);
Query OK, 1 row affected (0.21 sec)
mysql> insert into DemoTable1597 values(59);
Query OK, 1 row affected (0.24 sec)
mysql> insert into DemoTable1597 values(43);
Query OK, 1 row affected (0.11 sec)
mysql> insert into DemoTable1597 values(85);
Query OK, 1 row affected (0.17 sec)
mysql> insert into DemoTable1597 values(89);
Query OK, 1 row affected (0.12 sec)

Display all records from the table using select statement −

mysql> select * from DemoTable1597;

This will produce the following output −

+-------+
| Marks |
+-------+
|    45 |
|    59 |
|    43 |
|    85 |
|    89 |
+-------+
5 rows in set (0.00 sec)

Here is the query to remove ERROR 1093 (HY000). We are deleting the lowest value here −

mysql> delete from DemoTable1597
   -> where Marks=(
   -> select lowestMarks from ( select min(Marks) as lowestMarks from DemoTable1597 ) as deleteRecord
   -> ) limit 1;
Query OK, 1 row affected (0.11 sec)

Let us check the table records once again −

mysql> select * from DemoTable1597;

This will produce the following output −

+-------+
| Marks |
+-------+
|    45 |
|    59 |
|    85 |
|    89 |
+-------+
4 rows in set (0.00 sec)

Advertisements