Last active
February 17, 2017 06:25
-
-
Save KennethanCeyer/153b5b32bdaa73c0185d88c7fdbd03af to your computer and use it in GitHub Desktop.
MySQL_CAST_ISSUE When using in `where` clause of delete.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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