You could not use a subquery in Top
Top
syntax
[
TOP (expression) [PERCENT]
[ WITH TIES ]
]
expression
Is the numeric expression that specifies the number of rows to be returned. expression is implicitly converted to a float value if PERCENT is specified; otherwise, it is converted to bigint.
We can see it didn't support subquery in TOP
only expression
EDIT
I saw you change your use dbms.
Mysql didn't support TOP
but you can use LIMIT
get the limit row.
If there isn't PK
in your table, you can try to use dynamic SQL
get to decide how many rows you want to delete in the runtime.
Schema (MySQL v5.6)
CREATE TABLE tests(
test_name VARCHAR(50),
product_id int
);
INSERT INTO tests VALUES('WALLET_01',25);
INSERT INTO tests VALUES('WALLET_01',25);
SET @sql = NULL;
SET @Rn = NULL;
SELECT
(COUNT(*)-1)
INTO @Rn
FROM tests
WHERE test_name='WALLET_01' AND product_id=25;
SET @sql = CONCAT('DELETE FROM tests WHERE test_name=''WALLET_01'' AND product_id=25 limit ',@Rn);
PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;
Query #1
SELECT * FROM tests;
| test_name | product_id |
| --------- | ---------- |
| WALLET_01 | 25 |
View on DB Fiddle