DQL( Data Query Language 数据查询语言 )
SELECT
语句。SELECT语法
SELECT [ALL | DISTINCT]
{
* | table.* | [table.field1[as alias1][,table.field2[as alias2]][,...]]}
FROM table_name [as table_alias]
[left | right | inner join table_name2] -- 联合查询
[WHERE ...] -- 指定结果需满足的条件
[GROUP BY ...] -- 指定结果按照哪几个字段来分组
[HAVING] -- 过滤分组的记录必须满足的次要条件
[ORDER BY ...] -- 指定查询记录按一个或多个条件排序
[LIMIT {
[offset,]row_count | row_countOFFSET offset}];
-- 指定查询的记录从哪条至哪条
注意 : [ ] 括号代表可选的 , { }括号代表必选得
测试示例如下:
-- 创建有一个 jdbcStudy 的数据库,并创建 users 表同时进行插入数据
CREATE DATABASE jdbcStudy CHARACTER SET utf8 COLLATE utf8_general_ci;
-- 指定到新创建的 jdbcStudy 数据库
use jdbcStudy;
-- 创建 users 表
CREATE TABLE `users`(
id INT PRIMARY KEY,
NAME VARCHAR(40),
PASSWORD VARCHAR(40),
email VARCHAR(60),
birthday DATE
);
-- 插入数据
INSERT INTO `users`(id,NAME,PASSWORD,email,birthday)
VALUES(1,'zhansan','123456','[email protected]','1980-12-04'),
(2,'lisi','123456','[email protected]','1981-12-04'),
(3,'wangwu','123456','[email protected]','1979-12-04'),
(4,'wangwu','123456','[email protected]','1979-12-04')
查询所有数据,使用SELECT * FROM users
显示结果如下:
mysql> SELECT * FROM users;
+----+---------+----------+-----------------+------------+
| id | NAME | PASSWORD | email | birthday |
+----+---------+----------+-----------------+------------+
| 1 | zhansan | 123456 | zs@sina.com | 1980-12-04 |
| 2 | lisi | 123456 | lisi@sina.com | 1981-12-04 |
| 3 | wangwu | 123456 | wangwu@sina.com | 1979-12-04 |
| 4 | wangwu | 123456 | wangwu@sina.com | 1979-12-04 |
+----+---------+----------+-----------------+------------+
4 rows in set (0.00 sec)
语法:select 字段名 from 表名
查询指定列(姓名,生日),使用SELECT NAME,birthday FROM users
,结果如下所示:
mysql> SELECT NAME,birthday FROM users;
+---------+------------+
| NAME | birthday |
+---------+------------+
| zhansan | 1980-12-04 |
| lisi | 1981-12-04 |
| wangwu | 1979-12-04 |
| wangwu | 1979-12-04 |
+---------+------------+
4 rows in set (0.00 sec)
语法:字段名 或者表名 AS 新名字,AS 可以省略不写。
作用:
-- 这里是为列取别名(当然as关键词可以省略)
SELECT NAME AS 姓名,birthday AS 生日 FROM users;
-- 使用as也可以为表取别名,此处暂时看不出有什么作用,但是在多表查询的时候使用 as 关键字取别名可以更高效
SELECT NAME AS 姓名,birthday AS 生日 FROM users as user;
测试结果如下所示:
mysql> SELECT NAME AS 姓名,birthday AS 生日 FROM users;
+---------+------------+
| 姓名 | 生日 |
+---------+------------+
| zhansan | 1980-12-04 |
| lisi | 1981-12-04 |
| wangwu | 1979-12-04 |
| wangwu | 1979-12-04 |
+---------+------------+
4 rows in set (0.02 sec)
mysql> SELECT NAME AS 姓名,birthday AS 生日 FROM users as user;
+---------+------------+
| 姓名 | 生日 |
+---------+------------+
| zhansan | 1980-12-04 |
| lisi | 1981-12-04 |
| wangwu | 1979-12-04 |
| wangwu | 1979-12-04 |
+---------+------------+
4 rows in set (0.00 sec)
作用 : 去掉SELECT查询返回的记录结果中重复的记录 ( 返回所有列的值都相同 ) , 只返回一条
-- 查看有哪些用户名
SELECT NAME FROM users;
-- 查看有哪些用户名并去除重复
SELECT DISTINCT NAME FROM users;
测试结果如下所示:
mysql> SELECT NAME FROM users;
+---------+
| NAME |
+---------+
| zhansan |
| lisi |
| wangwu |
| wangwu |
+---------+
4 rows in set (0.00 sec)
mysql> SELECT DISTINCT NAME FROM users;
+---------+
| NAME |
+---------+
| zhansan |
| lisi |
| wangwu |
+---------+
3 rows in set (0.05 sec)