简体   繁体   English

使用 sql 删除记录,如果它大于某个数字并附加一个单位(例如 3M 或 3cm)

[英]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 15M
4M 4M
8M 8M
3M 3M
1M 1M
2M 2M
11M 11M

how do i delete the records in the table using sql,where worth is greater than 3M.如何使用 sql 删除表中的记录,其中价值大于 3M。

In the WHERE clause of the DELETE statement you can convert the string to a numeric value by adding 0 :DELETE语句的WHERE子句中,您可以通过添加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:如果您只想删除后缀为M的行(如果还有其他后缀),请添加:

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

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.检索不包括最后一个字符的列的数据,即(从 10M 到 10),然后 + 0 将其转换为 int。

So the > 3 comparison at the end basically means selecting data that are greater than 3M.所以最后的> 3比较基本上就是选择大于3M的数据。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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