我正在制作“搜索广告资源”网页,需要一些帮助。
此查询效果很好:
SELECT TOP (20) [FederalStockNum], [Quant1]
FROM [FederalStockCards-Detail]
WHERE (FederalStockNum LIKE '%SEARCH-KEYWORD%')
它会搜索基于SEARCH-KEYWORD的类似结果的广告资源,并返回如下内容:
______________________________________
| Federal Stock Number......|Quantity.|
|_____________________________________|
| 5305-00-060-9995..........|.......24|
| 5305-00-060-9996..........|.....5500|
| MS15795-543...............|.......50|
| MS21098-83................|........0|
到目前为止一切顺利,但这不足以揭露该公司的确切库存。我可以更改SQL语句以返回这样的内容吗?
______________________________________
| Federal Stock Number......|Quantity.|
|_____________________________________|
| 5305-00-060-9995..........|IN STOCK |
| 5305-00-060-9996..........|IN STOCK |
| MS15795-543...............|IN STOCK |
| MS21098-83................|SOLD OUT |
答案 0 :(得分:1)
您可以使用CASE
expression:
SELECT TOP (20) [FederalStockNum],
(case when [Quant1]>0 then 'IN STOCK' else 'OUT OF STOCK' end) as Quant1
FROM [FederalStockCards-Detail]
WHERE (FederalStockNum LIKE '%SEARCH-KEYWORD%')
如果您想使用聚合函数(例如GROUP BY
和SUM
),这也可以。