免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 940 | 回复: 0
打印 上一主题 下一主题

MYSQL 教程:§6, 查询 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2008-02-16 16:32 |只看该作者 |倒序浏览

§6, 查询
§6.1  查询简介

常用的查询格式如下:
SELECT columnsFROM tables[WHERE conditions][GROUP BY group[HAVING group_conditions]][ORDER BY sort_columns][LIMIT limits];

Select的语法下一章介绍。

跨数据库的查询方式:
select namefrom employee.employee;select employee.employee.namefrom employee;
后者要求选定数据库employee,前者只要进入mysql就可以。

§6.2  别名


可以使用别名,如下
mysql> select name as
employeeName
    -> from employee;
+---------------+
| employeeName  |
+---------------+
| Ajay Patel    |
| Nora Edwards  |
| Candy Burnett |
| Ben Smith     |
| Meil          |
| 0             |
+---------------+
6 rows in set (0.00 sec)
别名在多表查询时使用会比较多。
select e.namefrom employee as e;
别名中的as是可以省略的,这样做是为了和其他语言兼容。



select e.namefrom employee as e;


§6.3  使用WHERE     
进行查询
select employeeID, namefrom employeewhere job='Programmer';
+------------+--------------+
| employeeID | name         |
+------------+--------------+
|       6651 | Ajay Patel   |
|       7513 | Nora Edwards |
+------------+--------------+
2 rows in set (0.00 sec)

注意其他语言中使用==来判断等于,这里仅仅有=。其他符号还有!= or ,>,,>=,,IS NULL,IS NOT
NULL, AND, OR, and NOT,count()

select count(*) from employee;

mysql>
select count(*) from employee;
+----------+
| count(*) |
+----------+
|        6 |
+----------+
1 row in set
(0.00 sec)


使用where 子句的时候不允许使用列别名,这是ANSI SQL的限制。因为检查WHERE条件时,还未必知道这个别名。
§6.4  消除重复
select jobfrom employee;
执行结果:
+-----------------------+
| job                   |
+-----------------------+
| Programmer            |
| Programmer            |
| Systems Administrator |
| DBA                   |
| NULL  
               |
| NULL                  |
+-----------------------+

select distinct jobfrom employee;执行结果

+-----------------------+
| job                   |
+-----------------------+
| Programmer            |
| Systems Administrator |
| DBA                   |
| NULL                  |
+-----------------------+
select count(distinct job) from employee;

select count(job) from employee;


§6.4  使用GROUP BY
       GROUP
BY 按组来获取数据,一般与函数配合使用.
mysql> select count(*), job
   
-> from employee
   
-> group by job;
+----------+-----------------------+
| count(*) | job                   |
+----------+-----------------------+
|      
1 | DBA                   |
|      
2 | Programmer            |
|      
1 | Systems Administrator |
+----------+-----------------------+
3 rows in set (0.00 sec)

ANSI SQL 和MYSQL处理GROUP BY不同.前者要根据select列中选出来的所有列来分组.MYSQL则可以,甚至还可以给分组排序.
mysql> select count(*), job
   
-> from employee
   
-> group by job desc;
+----------+-----------------------+
| count(*) | job                   |
+----------+-----------------------+
|      
1 | Systems Administrator |
|      
2 | Programmer            |
|      
1 | DBA                   |
+----------+-----------------------+
3 rows in set (0.00 sec)


§6.5  HAVING

HAVING 一般是针对组,它是组中的where语句.
select count(*), jobfrom employeegroup by jobhaving count(*)=1;以上为雇员职业数为1的查询   
§6.6  使用ORDER
BY排序

select *from employeeorder by job asc, name desc;

执行结果
+-------------+---------------+-----------------------+---------------+| employeeID  | name          | job                   | departmentID  |+-------------+---------------+-----------------------+---------------+|        9842 | Ben Smith     | DBA                   |            42 ||        7513 | Nora Edwards  | Programmer            |           128 ||        6651 | Ajay Patel    | Programmer            |           128 ||        9006 | Candy Burnett | Systems Administrator |           128 |+-------------+---------------+-----------------------+---------------+4 rows in set (0.02 sec)


§6.6  使用LIMIT限制查询结果

选取6-8行,可以和order by配合选取最后面的select *from employeeSkillslimit 5, 3; 从第5个开始,选3个,即选取6-8,是从0开始计数的.
§6.7  小结
The SELECT statement has the following
general form:



SELECT columns
FROM tables
[WHERE conditions]
[GROUP BY group
[HAVING group_conditions]]
[ORDER BY sort_columns]
[LIMIT limits];

The clause select * retrieves all columns;
select columnname retrieves a particular column.

We can specify tables as database.table and
columns as table.column or database.table.column to avoid ambiguity.

Aliases are alternative names for tables
and columns. Specify them this way:



select column as column_alias
from table as table_alias;

The WHERE clause is used to select rows
matching search criteria.

The keyword DISTINCT removes duplicates
from the result set.

The GROUP BY clause treats the rows
retrieved group by group. Its chief use is in conjunction with group functions
like count().

The HAVING clause is like a WHERE clause
for groups.

The ORDER BY clause sorts result rows
according to the columns you specify.

The LIMIT clause is used to control which
rows are returned from the total possible result set. You can specify the
maximum rows returned and an offset from which to start.
               
               
               

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/21908/showart_479287.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP