【问题标题】:SQL - Like and greater than FunctionSQL - 喜欢和大于函数
【发布时间】:2021-08-31 18:25:54
【问题描述】:

我有一个如下数据集:

table_a

Product_Name      Product_Orders
    game_296                   1
    game_298                   2
    game_299                   4
    300_game                   6
    xyz_game                   9
    game-tyw                  12

如何在 SQL 中使用 like 函数并与大于组合?我的总体目标是过滤大于某个数字(例如 297)的游戏。

理想情况下,我想做这样的事情:

select * from table_a
where Product_Name > ilike %297%

这里的预期输出是这样的:

Product_Name      Product_Orders
    game_298                   2
    game_299                   4
    300_game                   6

【问题讨论】:

  • 我想知道的是它是否是一个支持 REGEXP_SUBSTR() 的数据库管理系统,例如 Oracle、SQL Server、Redshift 和 Vertica。因此,提及您正在使用的数据库管理系统会有所帮助。
  • 雪花数据库

标签: sql database snowflake-cloud-data-platform sql-like


【解决方案1】:

一种方法是从字符串中删除所有非数字,然后进行比较:

where cast(regexp_replace(product_name, '[^0-9]', '') as int) > 297

【讨论】:

  • 这可能与 99game99 将匹配的警告有关
  • @9bO3av5fw5 。 . . OP完全不清楚这是否正确。
  • 这适用于我正在寻找的示例,谢谢!
【解决方案2】:

试试这个:

-- your input ....
WITH
indata(Product_Name,Product_Orders) AS (
          SELECT 'game_296', 1
UNION ALL SELECT 'game_298', 2
UNION ALL SELECT 'game_299', 4
UNION ALL SELECT '300_game', 6
UNION ALL SELECT 'xyz_game', 9
UNION ALL SELECT 'game-tyw',12
)
-- real query starts here ...
SELECT
  product_name
, product_orders
FROM indata
WHERE CAST(REGEXP_SUBSTR(product_name,'\d+') AS INTEGER) > 297;
-- out  product_name | product_orders 
-- out --------------+----------------
-- out  game_298     |              2
-- out  game_299     |              4
-- out  300_game     |              6
-- out (3 rows)

【讨论】:

    【解决方案3】:

    可能是这样的:

    select * from table_a
    where Product_Name like %[2][9][8-9]% or Product_Name like %[3-9][0-9][0-9]%
    

    我不知道你使用的SQL,它可能是语法错误。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-21
      • 1970-01-01
      • 2020-05-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-03-18
      相关资源
      最近更新 更多