2 掌握MySQL的SELECT语法

2.1 阅读文档,找到SELECT的语法

http://dev.mysql.com/doc/refman/5.1/zh/sql-syntax.html#select

2.2 读SELECT的说明文档

2.3 实践文档提及的SELECT子句的功能,并提交自己的练习SQL和结果

mysql> select * from students;
+----+-------+----------+------------+
| id | name  | addresss | year       |
+----+-------+----------+------------+
|  1 | allen | Beijing  | 1980-01-02 |
|  2 | pual  | Shanghai | 1980-01-02 |
|  3 | john  | dalian   | 1980-01-02 |
+----+-------+----------+------------+
3 rows in set (0.00 sec)

mysql> select name,year from students;
+-------+------------+
| name  | year       |
+-------+------------+
| allen | 1980-01-02 |
| pual  | 1980-01-02 |
| john  | 1980-01-02 |
+-------+------------+
3 rows in set (0.00 sec)

mysql> select concat(name, ',', year) string from students order by string;
+------------------+
| string           |
+------------------+
| allen,1980-01-02 |
| john,1980-01-02  |
| pual,1980-01-02  |
+------------------+
3 rows in set (0.00 sec

mysql> select t1.name, t2.name from students as t1, teachers as t2 where t1.name = t2.name;
+------+------+
| name | name |
+------+------+
| john | john |
+------+------+
1 row in set (0.00 sec)

mysql> select name, year, id from students order by name, year;
+-------+------------+----+
| name  | year       | id |
+-------+------------+----+
| allen | 1980-01-02 |  1 |
| john  | 1980-01-02 |  3 |
| pual  | 1980-01-02 |  2 |
+-------+------------+----+
3 rows in set (0.00 sec)


mysql> select name, year, id from students order by name DESC, year;
+-------+------------+----+
| name  | year       | id |
+-------+------------+----+
| pual  | 1980-01-02 |  2 |
| john  | 1980-01-02 |  3 |
| allen | 1980-01-02 |  1 |
+-------+------------+----+
3 rows in set (0.00 sec)


mysql> select name, count(year), id from students group by id;
+-------+-------------+----+
| name  | count(year) | id |
+-------+-------------+----+
| allen |           1 |  1 |
| pual  |           1 |  2 |
| john  |           1 |  3 |
+-------+-------------+----+
3 rows in set (0.00 sec)






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值