【问题标题】:SQL result from delimited list来自分隔列表的 SQL 结果
【发布时间】:2021-12-31 12:01:53
【问题描述】:

'serial' 列包含多个以冒号 ':' 分隔的值。它是一个分隔符列表(由 :) 我正在使用“where”子句和“like”运算符来检索我所需的序列号,并且我只想在结果列中显示该特定序列号。

我的示例表:

serial      |name  | description
--------------------------------
123:234:456 |Raj   | Temporary
111:222:333 |Kumar | Permanent

我的示例代码:

select serial,name from table
where serial like '%234%'

我目前的结果:

serial      | name
------------------
123:234:456 | Raj

预期结果:

serial  | name
--------------
234     | Raj

【问题讨论】:

  • select 234 serial, name from table where concat(':',serial,':') like '%:234:%'
  • 对于您当前的期望,您可以简单地使用select 234,name from table where serial like '%234%'。但是如果你只通过了'23'怎么办。那你的期望是什么?
  • 几分钟前你已经问过同样的问题,为什么要删除并重新发布?如果要显示传递给LIKE 的值,只需选择它。再说一次,为什么你期望LIKE 只返回匹配的部分?
  • 如您所知,这里的桌子设计很糟糕。有一些方法可以编写查询来解决它,但它们是特定于 RDBMS 的。您使用postgresqlmysqlsql-server 还是什么?请edit您的问题添加适当的标签。

标签: sql delimiter denormalization


【解决方案1】:

使用这个 sn-ps:

declare @cond varchar(10)
set @cond='123'
select @cond as serial,name 
FROM table where serial like '%'+@cond+'%'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-12-08
    • 1970-01-01
    • 1970-01-01
    • 2021-11-17
    相关资源
    最近更新 更多