我有幸运抽奖选项表,其中两种类型的选项是一种是库存数量选项,第二种选择是一般选项
现在我必须在获得随机选项之前检查股票数量
我已经写了如下查询
set @percentage = 100 * rand();
select bkt_lucky_option.*,bkt_lucky_option_stock.option_stock_qty
from bkt_lucky_option LEFT JOIN bkt_lucky_option_stock ON bkt_lucky_option.lucky_option_id = bkt_lucky_option_stock.option_lucky_id
WHERE bkt_lucky_option_stock.option_stock_qty > 0
order by bkt_lucky_option.lucky_option_in_hour <= @percentage, rand()
limit 0, 8
WHERE bkt_lucky_option_stock.option_stock_qty&gt; 0
对于这种情况,我必须检查 lucky_option_type 是否为1然后才应该应用此条件
答案 0 :(得分:1)
WHERE lucky_option_type != 1 OR bkt_lucky_option_stock.option_stock_qty > 0
答案 1 :(得分:1)
如果您使用的是SQL Server,请尝试以下查询:
SET @percentage = 100 * rand();
SELECT bkt_lucky_option.*,bkt_lucky_option_stock.option_stock_qty
FROM bkt_lucky_option
LEFT JOIN bkt_lucky_option_stock ON bkt_lucky_option.lucky_option_id =
bkt_lucky_option_stock.option_lucky_id
WHERE ( bkt_lucky_option_stock.option_stock_qty > 0 AND lucky_option_type =
1 ) OR
( bkt_lucky_option_stock.option_stock_qty =
bkt_lucky_option_stock.option_stock_qty AND lucky_option_type <> 1
)
ORDER BY bkt_lucky_option.lucky_option_in_hour <= @percentage, RAND()
limit 0, 8