select 语句
1.检索单个列
select <列名> from <表名>;
//注意,' ; '用于命令行中结束语句,DBMS中可不加
2.检索多个列
select <column>,<~>,<~~> from <table>
//返回的数据一般是原格式,不改变精度或货币表示
3.检索所有列
用*通配符,* 表示模糊查询
select * from <table name>;
最好不要使用 ‘ * ’,会影响数据库的效率。
4.检索不同 行(去重)
用distinct 关键字
mysql> SELECT age FROM tb_students_info;
+------+
| age |
+------+
| 25 |
| 23 |
| 23 |
| 22 |
| 24 |
| 21 |
| 22 |
| 23 |
| 22 |
| 23 |
+------+
10 rows in set (0.00 sec)
mysql> SELECT DISTINCT age
-> FROM tb_students_info;
+------+
| age |
+------+
| 25 |
| 23 |
| 22 |
| 24 |
| 21 |
+------+
5 rows in set (0.11 sec)
(ps)
distinct 应用于全部的列,不仅是它的前置列
select distinct name,id
5.限制检索结果
用 limit 子句
select <column> from <table> limit 5;
limit 10,5 指返回 11,12,13,14,15行
6.完全限制表名
//某些情节需要
select text1.score from course.text1;
从course数据库中的text1表,检索出score列
**
排序检索数据
**
select first_name<列> from employees<表名>
order by first_name;
order by <列名>默认为升序排列(ASC),
也可以指定降序(DESC);
select salary from employees order by name DESC;
按薪资降序输出.
select salary , name from employees order by salary DESC LIMIT 1;
输出工资最高的人的名字和工资。
ps
from 语句应在***order by*** 之前,
order by 应在 limit 之前