sql - How to get row value from comma's values
问题描述
Below is my Table XYZ data
Sno ProgramID ProgramName
1 1,2 CCE
2 3,11 DDU
I want output like below passing programID 1
Sno ProgramID ProgramName
1 1,2 CCE
I am trying query like below
select * from XYZ where ProgramID in('1')
No result is coming from above query, my question is single value matching with in query. I dont want like query match with exact value
解决方案
You have a broken data model. You can do what you want using LIKE
:
select *
from XYZ
where ',' + ProgramID + ',' like '%,1,%';
That said, you should understand why storing multiple values in a string is wrong:
- Numbers should be stored as numbers, not strings.
- Ids should have properly declared foreign key references, which you cannot do with a string.
- SQL has poor string processing capabilities.
- Queries cannot make use of indexes and partitions and probably confuse the optimizer.
- Maintaining the string to avoid duplicates is tricky.
- SQL has a great data type for storing lists. It is called a table not a string.
推荐阅读
- objective-c - 从根对象获取引用
- airflow - 如何在分组任务中定义 Airflow 依赖项?
- javascript - 在 javascript 中从 Fetched Api 下载渲染图像的任何函数
- python - Discord.py 斜线命令在 cogs 中不起作用
- laravel - 如何在 laravel + vue 中 foreach 数组?
- postgresql - 复制滞后 - 超过 max_slot_wal_keep_size,未删除 WAL 段
- javascript - 如何使用 socket.io 客户端从 websocket 升级请求中获取状态码
- avif - 什么是 avif 文件签名?
- r - 格式表中的页长选项?除了尝试使用 as.datatable 之外,还有其他的吗?
- vue.js - 如何在应用程序中注册翻译功能