在SQL 脚本中如何使用LIKE嵌套子查询

来源:百度知道 编辑:UC知道 时间:2024/05/18 04:38:56
商品表
ProducutID ProductCategory Productname
1 123 商品1
类别表
ProductCategoryId ProductCategoryName
1 类别1
2 类别2
3 类别3

如何查询出商品所在的所有类别

ProductCategory 123 是一个类别?还是1、2、3类别都包括了?
如果是一个类别:
select * from 类别表 where ProductCategoryId =
(select ProductCategory from 商品表 where Productname ='商品1')

如果商品1同时属于1、2、3三个类别,建议在商品表ProductCategory 字段不要存储为123而改为1,2,3,这样便于查询:
select * from 类别表 where ProductCategoryId in
(select ProductCategory from 商品表 where Productname ='商品1')