我在编写这个查询时遇到了很大的问题。
如果我的样本数据看起来像这样......
id Fruit name Group Type
-----------------------------------------
1 Orange Citrus null
2 Mango Sweet null
3 Red chilly Hot null
4 Green chilly Hot null
5 Green chilly Hot Organic 1
6 Green chilly Hot Organic 2
我想创建一个接受@FoodType参数的存储过程。
- 当
@FoodType作为 传递时NULL,SP 应该返回行1, 2, 3和4。 - 如果
@FoodType是Organic 2,那么 SP 应该返回1, 2, 3和6。
在我的表中,FruitName列Type可能会创建一个复合唯一键,但我没有创建一个。
编写此类查询的最佳方法是什么?
更新:
当传递“有机 2”时,由于没有为第 4 行定义类型,因此第 6 行优先于第 4 行。

