我在表 USER_TABLE 中有如下資料:
-----------------
| USERS_RANGE |
-----------------
| 5-98 |
| 9854-98666620 |
| 54-986 |
| 1-20 |
| 10000-122222220 |
| 10-1222 |
-----------------
我的要求聽到:
例如:-如果我搜索“7”,它應該顯示為:
-------------
| USERS_RANGE |
-------------
| 5-98 |
| 1-20 |
-------------
我嘗試了 ex:-select * from USER_TABLE WHERE USERS_RANGE <= '7';
以及 java 中的一些更多數學問題是否有任何查詢可以獲取這樣的資料。
uj5u.com熱心網友回復:
SUBSTRING_INDEX()我們可以使用子查詢方法和函式來獲取所需的資料。
/* select all inside subquery and filter with WHERE*/
SELECT USERS_RANGE FROM (
/* select all range and split the range between '-' */
SELECT
USERS_RANGE
, SUBSTRING_INDEX(USERS_RANGE, '-', 1) AS `from`
, SUBSTRING_INDEX(USERS_RANGE, '-', -1) AS `to`
FROM USERS_RANGE_TABLE
) A
WHERE A.from <= 7 AND A.to >= 7;
- 子查詢將選擇所有 USER_RANGE 資料,拆分范圍并使用別名
from和創建一個新列to, - 并且主查詢將選擇所有內部子查詢并使用 where by column
from和過濾結果to。
希望這有助于回答您的問題。
參考:
- https://dev.mysql.com/doc/refman/8.0/en/string-functions.html#function_substring-index
- https://dev.mysql.com/doc/refman/8.0/en/subqueries.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/516992.html
標籤:mysqlsql
下一篇:Laravel雄辯的麻煩
