简体   繁体   中英

Delete records using sql ,if it is greater than certain number with an unit attached to it (eg 3M or 3cm)

let a table be 'hotel', with a column:

hotel_worth
15M
4M
8M
3M
1M
2M
11M

how do i delete the records in the table using sql,where worth is greater than 3M.

In the WHERE clause of the DELETE statement you can convert the string to a numeric value by adding 0 :

DELETE FROM hotel
WHERE hotel_worth + 0 > 3

If you want to delete only rows with the suffix M (if there are other suffixes also) add:

AND RIGHT(hotel_worth, 1) = 'M'

This can be done by using:

DELETE FROM price 
WHERE SUBSTR(`hotel_worth`, 1, LENGTH(`hotel_worth`) - 1) + 0 > 3

Explanation: the substr function:

SUBSTR(`hotel_worth`, 1, LENGTH(`hotel_worth`) - 1)

retrieves the data of the column excluding the last character ie (from 10M to 10), then the + 0 to it converts it to int.

So the > 3 comparison at the end basically means selecting data that are greater than 3M.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM