SQL SELECT基本语句结构

(1)SELECT select_list

(2)  FROM table_list

(3)    WHERE search_conditions

      GROUP BY group_by_list

      HAVING search_conditions

(4)  ORDER BY order_list

实例:

/*USE master;
GO
IF DB_ID(N'mytest') IS NOT NULL
    DROP DATABASE mytest;
GO
CREATE DATABASE mytest;
GO

USE mytest;
GO
IF OBJECT_ID(N'dbo.Oreders') IS NOT NULL
    DROP TABLE dbo.Orders;
GO

CREATE TABLE dbo.Orders
(
    ProductID INT NOT NULL,
    MadeFrom CHAR(20),
    Sales MONEY NOT NULL
);


INSERT INTO dbo.Orders
VALUES (10,'China',100.00),
    (20,'US',500.00),
    (13,'China',10.00),
    (3,'UK',80.00),
    (2,'UK',90.00),
    (1,'US',320.00),
    (12,'Ger',214.00),
    (18,'Ger',125.00),
    (7,'JP',69.00),
    (1,'China',70.00),
    (5,'UK',560.00),
    (21,'China',211.00),
    (4,'China',89.00),
    (14,'China',100.00),
    (19,'China',100.00),
    (13,'China',100.00),
    (17,'China',100.00),
    (22,'China',100.00),
    (11,'China',100.00),
    (2,'China',100.00),
    (1,'China',100.00),
    (22,'China',100.00);
...
*/

SELECT ProductID, SUM(Sales) AS TotalSales 
    FROM dbo.Orders
    WHERE MadeFrom = N'China'
    GROUP BY ProductID
    HAVING SUM(Sales) > 150
    ORDER BY ProductID;

 

结果图:

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值