要灵活应用select子句首先我们要知道select子句中where、group by、 select、 from、 having、 order by、 limit各个关键字执行的顺序。下面给出他们的顺序,如果有不对的,欢迎指正。
Oracle11g中的select执行顺序:
具体实例 | SQL语句 |
---|---|
在产品表中,检索每一个供应商提供的商品的数量。 | mysql> SELECT vend_id,COUNT(*) AS num_prods FROM products GROUP BY vend_id; |
在订单表中,检索出具有两个以上订单的客户id以及订单数量。 | mysql> SELECT cust_id,COUNT(*) AS orders FROM orders GROUP BY cust_id HAVING orders>=2; |
在产品表中检索出能提供2个以上商品,并且价格高于10的供应商。 | mysql> SELECT vend_id,COUNT() AS num_prods FROM products WHERE prod_price>=10 GROUP BY vend_id HAVING COUNT()>=2; |
在订单明细表中,检索出订单总价格高于等于50的订单号以及订单总价格 | mysql> SELECT order_num,SUM(quantityitem_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantityitem_price)>=50 ORDER BY ordertotal; |
在订单细表中,检索出销量排名前十的店铺以及店铺销量 | select shop_id, count(*) as num from order where 1 group by shop_id oder by num limit 10; |