INSERT INTO 语句
INSERT INTO 语句用于向表格中插入新的行。
语法
INSERT INTO 表名称 VALUES (值1, 值2,....)
我们也可以指定所要插入数据的列:
INSERT INTO table_name (列1, 列2,...) VALUES (值1, 值2,....)
SQL COUNT() 函数
SQL COUNT() 语法
SQL COUNT(column_name) 语法
COUNT(column_name) 函数返回指定列的值的数目(NULL 不计入):
SELECT COUNT(column_name) FROM table_name
SQL COUNT(*) 语法
COUNT(*) 函数返回表中的记录数:
SELECT COUNT(*) FROM table_name
SQL COUNT(DISTINCT column_name) 语法
COUNT(DISTINCT column_name) 函数返回指定列的不同值的数目:
SELECT COUNT(DISTINCT column_name) FROM table_name
注释:COUNT(DISTINCT) 适用于 ORACLE 和 Microsoft SQL Server,但是无法用于 Microsoft Access。
SQL COUNT(column_name) 实例
我们拥有下列 "Orders" 表:
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 |
现在,我们希望计算客户 "Carter" 的订单数。
我们使用如下 SQL 语句:
SELECT COUNT(Customer) AS CustomerNilsen FROM Orders
WHERE Customer='Carter'
以上 SQL 语句的结果是 2,因为客户 Carter 共有 2 个订单:
SQL COUNT(*) 实例
如果我们省略 WHERE 子句,比如这样:
SELECT COUNT(*) AS NumberOfOrders FROM Orders
结果集类似这样:
这是表中的总行数。
SQL COUNT(DISTINCT column_name) 实例
现在,我们希望计算 "Orders" 表中不同客户的数目。
我们使用如下 SQL 语句:
SELECT COUNT(DISTINCT Customer) AS NumberOfCustomers FROM Orders
结果集类似这样:
这是 "Orders" 表中不同客户(Bush, Carter 和 Adams)的数目。
SQL ORDER BY 子句
排序
LIMIT
SELECT * FROM 表 LIMIT 12344, 1
注:
LIMIT 接受一个或两个数字参数。
参数必须是一个整数常量。
如果给定两个参数,第一个参数指定第一个返回记录行的偏移量,
第二个参数指定返回记录行的最大数目。
初始记录行的偏移量是 0(而不是 1)
Sql入门-----------组合多个查询
组合查询
一、union操作符
功能:组合两个或多个select语句的结果,不包括重复的记录。
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
- union
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
简单来说:使用union之后将两个select的结果放在一起来,但是重复的记录不会显示。
所以每个select语句里必须选择同样数量的字段、同样数量的字段表达式、同样的数据类型、同样的次序-----------但长度不必一样。
二、union all操作符
功能:组合两个select语句的结果,并且包括重复的结果。
· 与union区别,一个不返回重复值,一个返回重复值。
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
- union all
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
三、intersect操作符
功能:组合两个select语句,但只返回第一个select语句里与第二个select语句里一样的记录,使用规则与union一样。
但目前mysql5.0不支持intersect,sqlserver和oracle全部支持。
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
- intersect
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
四、except操作符
功能:组合两个select语句,返回第一个select语句里有但第二个select语句里没有的记录,使用规则与union一样。
但目前mysql不支持except,oracle使用minus操作符实现同样功能。
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
- except
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
附加:
1、组合查询里使用order by
order by子句可以用于组合查询,但只能用于对于全部查询结果的排序,只能有一个order by子句,而且只能以别名或数字来引用字段。
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
- operator { union | except | intersect | union all }
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
- [ order by ]
2、组合查询里使用group by
group by子句可以用于组合查询中的每一个select语句,也可用于全部查询结果,另外,having子句也可以用于组合查询里的每个select语句。
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
- [ group by ]
- [ having ]
- operator { union | except | intersect | union all }
- select column1 [ , column2 ]
- from table1 [ ,table2 ]
- [ where ]
- [ group by ]
- [ having ]
- [ order by ]
SQL多条件查询语句
我有一个表中 组号 姓名1 姓名2 姓名3 姓名4 和日期
我想查询 001组 小王 2013-4-15的记录 但是小王不一定在姓名1-4某个位置
怎样 筛选查询呢
select * from tab_name where 组号='001组' and to_char(日期,'yyyy-mm-dd')='2013-04-15' and 姓名1='小王'
union all
select * from tab_name where 组号='001组' and to_char(日期,'yyyy-mm-dd')='2013-04-15' and 姓名2='小王'
union all
select * from tab_name where 组号='001组' and to_char(日期,'yyyy-mm-dd')='2013-04-15' and 姓名3='小王'
union all
select * from tab_name where 组号='001组' and to_char(日期,'yyyy-mm-dd')='2013-04-15' and 姓名4='小王'
如何在SQL中按时间段查询数据
这样就可以了啊:
1.select *
from table_name where 发生日期 between '2008-07-01'and '2008-12-31'
2.和上面同理哈
求一sql语句,当列值为空不作查询条件,不为空作为查询条件!
select * from usertable
where (name=@name and page=@page ) or @name is null or @page is null
MySQL replace into 有三种形式:
1. replace into tbl_name(col_name, ...) values(...)
2. replace into tbl_name(col_name, ...) select ...
3. replace into tbl_name set col_name=value, ...
1.insert ignore into
当插入数据时,如出现错误时,如重复数据,将不返回错误,只以警告形式返回。所以使用ignore请确保语句本身没有问题,否则也会被忽略掉。例如:
INSERT IGNORE INTO books (name) VALUES ('MySQL Manual')
2.on duplicate key update
当primary或者unique重复时,则执行update语句,如update后为无用语句,如id=id,则同1功能相同,但错误不会被忽略掉。例如,为了实现name重复的数据插入不报错,可使用一下语句:
INSERT INTO books (name) VALUES ('MySQL Manual') ON duplicate KEY UPDATE id = id
3.insert … select … where not exist
根据select的条件判断是否插入,可以不光通过primary 和unique来判断,也可通过其它条件。例如:
INSERT INTO books (name) SELECT 'MySQL Manual' FROM dual WHERE NOT EXISTS (SELECT id FROM books WHERE id = 1)
4.replace into
如果存在primary or unique相同的记录,则先删除掉。再插入新记录。
REPLACE INTO books SELECT 1, 'MySQL Manual' FROM books