Menu
Woocommerce Menu

则 3522vip靠谱吗AND 运算符显示一条记录

0 Comment

合计函数 (比如 SUM) 常常需要添加 GROUP BY 语句。GROUP BY 语句GROUP BY
语句用于结合合计函数,根据一个或多个列对结果集进行分组。

AND 和 OR 运算符AND 和 OR 可在 WHERE 子语句中把两个或多个条件结合起来。

SQL GROUP BY 语法SELECT column_name,
aggregate_function(column_name)FROM table_nameWHERE column_name
operator valueGROUP BY column_nameSQL GROUP BY 实例我们拥有下面这个
Orders 表:

如果第一个条件和第二个条件都成立,则 AND 运算符显示一条记录。

O_Id OrderDate OrderPrice Customer 1 2008/12/29 1000 Bush 2 2008/11/23
1600 Carter 3 2008/10/05 700 Bush 4 2008/09/28 300 Bush 5 2008/08/06
2000 Adams 6 2008/07/21 100 Carter

如果第一个条件和第二个条件中只要有一个成立,则 OR 运算符显示一条记录。

现在,我们希望查找每个客户的总金额。

and运行符实例

我们想要使用 GROUP BY 语句对客户进行组合。

1 create table employee(2 ID int,3 name nvarchar (10),4 salary int,5
start_date datetime,6 city nvarchar (10),7 region char (1))8 GO12
insert into employee (ID, name, salary, start_date, city, region)3
values (1, ‘Jason’, 40420, ’02/01/94′, ‘New York’, ‘W’)4 GO

(1 rows affected)1 insert into employee (ID, name, salary,
start_date, city, region)2 values (2, ‘Robert’,14420, ’01/02/95′,
‘Vancouver’,’N’)3 GO

(1 rows affected)1 insert into employee (ID, name, salary,
start_date, city, region)2 values (3, ‘Celia’, 24020, ’12/03/96′,
‘Toronto’, ‘W’)3 GO

(1 rows affected)1 insert into employee (ID, name, salary,
start_date, city, region)2 values (4, ‘Linda’, 40620, ’11/04/97′,
‘New York’, ‘N’)3 GO

(1 rows affected)1 insert into employee (ID, name, salary,
start_date, city, region)2 values (5, ‘David’, 80026, ’10/05/98′,
‘Vancouver’,’W’)3 GO

(1 rows affected)1 insert into employee (ID, name, salary,
start_date, city, region)2 values (6, ‘James’, 70060, ’09/06/99′,
‘Toronto’, ‘N’)3 GO

(1 rows affected)1 insert into employee (ID, name, salary,
start_date, city, region)2 values (7, ‘Alison’,90620, ’08/07/00′,
‘New York’, ‘W’)3 GO

(1 rows affected)1 insert into employee (ID, name, salary,
start_date, city, region)2 values (8, ‘Chris’, 26020, ’07/08/01′,
‘Vancouver’,’N’)3 GO

(1 rows affected)1 insert into employee (ID, name, salary,
start_date, city, region)2 values (9, ‘Mary’, 60020, ’06/09/02′,
‘Toronto’, ‘W’)3 GO

(1 rows affected)12 select * from employee3 GOID name salary
start_date city region———– ———- ———–
———————– ———- —— 1 Jason 40420 1994-02-01
00:00:00.000 New York W 2 Robert 14420 1995-01-02 00:00:00.000
Vancouver N 3 Celia 24020 1996-12-03 00:00:00.000 Toronto W 4 Linda
40620 1997-11-04 00:00:00.000 New York N 5 David 80026 1998-10-05
00:00:00.000 Vancouver W 6 James 70060 1999-09-06 00:00:00.000 Toronto
N 7 Alison 90620 2000-08-07 00:00:00.000 New York W 8 Chris 26020
2001-07-08 00:00:00.000 Vancouver N 9 Mary 60020 2002-06-09
00:00:00.000 Toronto W

(9 rows affected)12 — Use AND operator in where clause34 SELECT Name,
ID5 FROM Employee6 WHERE Name Colin AND Name Martin7 GOName
ID———- ———–Jason 1Linda 4David 5James 6

我们使用下列 SQL 语句:

OR 运算符实例使用 OR 来显示所有姓为 Carter 或者名为 Thomas 的人:

SELECT Customer,SUM(OrderPrice) FROM OrdersGROUP BY
Customer结果集类似这样:

SELECT * FROM Persons WHERE firstname=’Thomas’ OR lastname=’Carter’

Customer SUM(OrderPrice) Bush 2000 Carter 1700 Adams 2000

我们同一条sql查询语句中可以用到and or同时操作如下

很棒吧,对不对?

实例

发表评论

电子邮件地址不会被公开。 必填项已用*标注

相关文章

网站地图xml地图