Skip to content

Instantly share code, notes, and snippets.

@KennethanCeyer
Last active February 17, 2017 06:25
Show Gist options
  • Save KennethanCeyer/153b5b32bdaa73c0185d88c7fdbd03af to your computer and use it in GitHub Desktop.
Save KennethanCeyer/153b5b32bdaa73c0185d88c7fdbd03af to your computer and use it in GitHub Desktop.
MySQL_CAST_ISSUE When using in `where` clause of delete.
DELETE
FROM `data_tbl_0`
WHERE
CAST(`col_37` AS DECIMAL(15, 6)) = 0 AND
ISNULL(`col_37`) <> TRUE
;
# The error will be occured.
# @ERROR Message: '(1366) Incorrect DECIMAL value: '0' for column '' at row -1
DELETE T
FROM (
SELECT *
FROM `data_tbl_0`
WHERE
CAST(`col_37` AS DECIMAL(15, 6)) = 0 AND
ISNULL(`col_37`) <> TRUE
) AS T
;
# @ERROR Message: '(1288): The target table T of the DELETE is not updatable.
# I think it seems like optimization plan of MySQL engine,
# The point is delete target and select target is same table, and this table will not be in derived table.
DELETE
FROM `data_tbl_0`
WHERE
`report_idx` IN (
SELECT `report_idx`
FROM `data_tbl_0`
WHERE
CAST(`col_37` AS DECIMAL(15, 6)) = 0 AND
ISNULL(`col_37`) <> TRUE
)
#ERROR Message: (1093): You can't specify target table `data_tbl_0` for update in FROM clause.
SELECT *
FROM `data_tbl_0`
WHERE
CAST(`col_37` AS DECIMAL(15, 6)) = 0 AND
ISNULL(`col_37`) <> TRUE
;
# col_37 dataset like this follows:
#
# PK | col_37
# 1 | '1'
# 2 | '2'
# 3 | 'a'
#
# And search result like this follows:
#
# PK | col_37
# 3 | 'a'
DELETE
FROM `data_tbl_0`
WHERE
`col_37` NOT REGEXP '^[0-9]+\\.?[0-9]*$'
;
# Use regular expression, This follows will be solved.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment