SQL:如何查找产品代码
我们有一个包含products表的数据库(MSSQL),其中产品以一种方式放置,即在产品id中,前4个字母是客户特定的代码。例如:SQL:如何查找产品代码,sql,sql-server,Sql,Sql Server,我们有一个包含products表的数据库(MSSQL),其中产品以一种方式放置,即在产品id中,前4个字母是客户特定的代码。例如: 123070004400B 002070004400B 012316003030E 等等 前4个字母是客户代码,后4个字母是可能重叠的产品代码。同一个客户可以订购代码相同的产品,但我们的库存是这样分开的。至少在数据库中 是否有一种查询方法可以告诉您,例如产品07004400B存在于多个客户条目中?使用“like”关键字查找ID以07004400B结尾的所有产品
- 123070004400B
- 002070004400B
- 012316003030E
select count(*) from TABLE where product_id like '%07004400B'
使用“like”关键字查找ID以07004400B结尾的所有产品
select count(*) from TABLE where product_id like '%07004400B'
使用substring()
提取产品代码,并根据需要查找的点击进行分组:
select substring(product_id, 5, len(product_id)) code
from products
group by substring(product_id, 5, len(product_id))
having count(*) > 1
如果需要特定的,请添加where子句:
select substring(product_id, 5, len(product_id)) code
from products
where substring(product_id, 5, len(product_id)) = '0700400B'
group by substring(product_id, 5, len(product_id))
having count(*) > 1
使用substring()
提取产品代码,并根据需要查找的点击进行分组:
select substring(product_id, 5, len(product_id)) code
from products
group by substring(product_id, 5, len(product_id))
having count(*) > 1
如果需要特定的,请添加where子句:
select substring(product_id, 5, len(product_id)) code
from products
where substring(product_id, 5, len(product_id)) = '0700400B'
group by substring(product_id, 5, len(product_id))
having count(*) > 1
substr
不是mysql函数吗?在MSSQL中,我认为它是子字符串
。对吗?substr
不是mysql函数吗?在MSSQL中,我认为它是子字符串
。对吗?