简体   繁体   English

SQL查询以asc desc顺序选择数据,并在字符串中查找数字

[英]Sql query to select data in asc desc order with find number in string

How can I find the number from string with a SQL query so I select data in asc and desc order with this number? 如何通过SQL查询从字符串中找到数字,以便以此数字按升序和降序选择数据?

I have table bf_offers and a column title with values like this: 我有表bf_offers和具有这样的值的列title

title
---------------
Flat 20% off
Flat 50% off
Upto 40% off

I want to find the number like 20, 50 & 40 and arrange title with in desc or asc order. 我想找到20、50和40之类的数字,并按desc或asc顺序排列标题。 If I set to desc order then data should be ordered like this: 如果我设置为desc顺序,那么数据应该像这样排序:

title
-----------------
Flat 50% off
Flat 40% off
Upto 20% off

你可以试试-

ORDER BY CAST(RIGHT(SUBSTRING_INDEX(title,'%',1),2) AS SIGNED) DESC

Using substring function do this. 使用子字符串函数执行此操作。

order by SUBSTRING(title, CHAR_LENGTH(title) - 5,2) 

or descending 或下降

order by SUBSTRING(title, CHAR_LENGTH(title) - 5,2) desc

Try following, 尝试跟随,

SELECT CAST((SELECT SUBSTRING(title, n, 1)
              FROM tablename
              WHERE n <= LEN(title)
                AND SUBSTRING(titale, n, 1) LIKE '[0-9]'
              FOR XML PATH('')) AS float)
FROM tablename

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

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