SELECT a.goods_code goodsCode, a.goods_name goodsName, a.goods_price goodsPrice, a.original_price originalPrice, IFNULL( a.initial_sales, 0 )+ IFNULL( a.real_sales, 0 ) sales, a.thumbnail, CASE WHEN a.exchange_type IS NULL OR a.exchange_type = 0 THEN '不限制' ELSE a.exchange_number END exchangeTypeName, (SELECT MIN(b.redeem_points) redeem_points FROM goods_attr_info b WHERE a.goods_code = b.goods_code ) redeem_points, CASE WHEN (SELECT MIN(b.inventory_number) <= 10 FROM goods_attr_info b WHERE a.goods_code = b.goods_code AND b.inventory_type = 1) THEN CONCAT('剩余',MIN(b.inventory_number),'件') ELSE '' END inventoryNumber FROM goods_main_info a WHERE a.goods_status = 1 这断sql改造成上述语句
时间: 2024-03-27 20:37:30 浏览: 120
可以使用以下 SQL 语句实现:
```
SELECT a.goods_code AS goodsCode,
a.goods_name AS goodsName,
a.goods_price AS goodsPrice,
a.original_price AS originalPrice,
IFNULL(a.initial_sales, 0) + IFNULL(a.real_sales, 0) AS sales,
a.thumbnail,
CASE WHEN a.exchange_type IS NULL OR a.exchange_type = 0 THEN '不限制'
ELSE a.exchange_number END AS exchangeTypeName,
(SELECT MIN(b.redeem_points)
FROM goods_attr_info b
WHERE a.goods_code = b.goods_code) AS redeem_points,
CASE WHEN (SELECT MIN(b.inventory_number)
FROM goods_attr_info b
WHERE a.goods_code = b.goods_code AND b.inventory_type = 1) <= 10
THEN CONCAT('剩余', MIN(b.inventory_number), '件')
ELSE '' END AS inventoryNumber
FROM goods_main_info a
WHERE a.goods_status = 1;
```
这个语句的逻辑与之前的语句相同,只是将子查询部分的逻辑拆分出来,分别使用了两个子查询来计算 `redeem_points` 和 `inventoryNumber`,并且使用了 `CASE WHEN` 语句来判断是否需要输出 `inventoryNumber`。
阅读全文