我从wordpress表中获得此查询。我正在与其他数据库一起在wordpress之外构建一个api。我正想购买产品库存 仅 。
因此-这是最大的情况-可以给出两个值,库存或缺货。
max( CASE WHEN pm.meta_key = "_stock_status" and product_id = pm.post_id THEN pm.meta_value END ) as product_stock_status
如何在此查询中仅获取库存值?
完整查询:
SELECT product_id, product_language, product_name,
max( CASE WHEN pm.meta_key = "_price" and product_id = pm.post_id THEN pm.meta_value END ) as product_price,
max( CASE WHEN pm.meta_key = "_stock_status" and product_id = pm.post_id THEN pm.meta_value END ) as product_stock_status
FROM search_table
INNER JOIN postmeta pm ON product_id = pm.post_id
WHERE product_language LIKE "en"
GROUP BY product_id
ORDER BY RAND() LIMIT 15
我希望获得 product_stock_status 库存的随机产品。
编辑:解决方案
在GROUP BY之后使用HAVING子句,即HAVING product_stock_status ='instock'–尼克
SELECT product_id, product_language, product_name,
max( CASE WHEN pm.meta_key = "_price" and product_id = pm.post_id THEN pm.meta_value END ) as product_price,
max( CASE WHEN pm.meta_key = "_stock_status" and product_id = pm.post_id THEN pm.meta_value END ) as product_stock_status
FROM search_table
INNER JOIN postmeta pm ON product_id = pm.post_id
WHERE product_language LIKE "en"
GROUP BY product_id
HAVING product_stock_status = 'instock'
ORDER BY RAND() LIMIT 15