Sql 选择X,其中X使用了Y>;1.
我是否可以进行计数以查找多次使用折扣代码的订单Sql 选择X,其中X使用了Y>;1.,sql,Sql,我是否可以进行计数以查找多次使用折扣代码的订单 Order Codes 123 mowerwo 151 femorfm 151 geirmgr 151 rtert 161 erger 161 wefww 162 wweff 所以我想让它带回订单:151和161 SELECT COUNT (discountcode) AS discount, order_num FROM
Order Codes
123 mowerwo
151 femorfm
151 geirmgr
151 rtert
161 erger
161 wefww
162 wweff
所以我想让它带回订单:151和161
SELECT COUNT (discountcode) AS discount, order_num
FROM ordersdiscount
HAVING COUNT (discountcode)>1
你可以这样做:
SELECT order_num
FROM orderdiscount
GROUP BY discountcode
HAVING COUNT (*) > 1;
如果您只需要代码不进行不必要的计数,则需要在
选择和分组依据列表(此处order_num
)中包含所有列,这不是一个聚合函数,如sum、count、avg等
count(*)
或count(1)
或count(0)
或count(order\u num)
都给出相同的结果
因此,请使用:
SELECT order_num, count(1) as discount
FROM ordersdiscount
GROUP BY order_num
HAVING count(1)>1;
order_num discount
151 3
161 2
在<代码>拥有
我的日子之前,你只是错过了一个按顺序分组\u num
。-为此干杯!无论如何都要计算count(*)
,因为您在select语句的have中使用它,count()计算返回的行数,count()在have count中,count()按statant计算相对于组的行数,他只想计算每个组@LamakNo的行数,您很困惑,是相同的count(*)
valuecount(折扣代码)
如果列具有NULL
值,则不会给出相同的结果,though@Lamak是的,需要说count(order\u num)
,好的,谢谢。