1. 基本查询
// 检索单个列
select prod_name from products;
// 检索所有列
select * from products;
// 检索不同的行
select distinct vend_id from products;
// 限制结果
select prod_name from products limit 4,5;
1.LIMIT :若有两个参数,则第一个参数为开始位置(从 0 行开始),第二个参数为返回结果的数量;若只有一个参数,则表示限制返回结果的数量。
2.除非确实需要表中的每个列,否则最好别使用 * 通配符,检索不需要的列会降低检索的性能。
2. 排序数据
// 对 `prod_name` 进行升序排序
select prod_name from products order by prod_name;
// 按多个列排序: 对 `prod_price` 降序排序,对 `prod_name` 升序排序
select prod_price, prod_name
form products
order by prod_price desc, prod_name;
如果没有明确规定排序顺序,MySQL 默认以数据添加到表中的顺序返回。
3.过滤数据
使用 WHERE 子句来指定搜索条件。
// 检查单个值,MySQL 在执行匹配时默认不区分大小写,因此 'jochen' 和 'Jochen' 都会匹配
select prod_name, prod_price from products where prod_name = 'jochen';
// 不匹配检查
select vend_id, prod_name from products where vend_id != 1000;
// 范围检查
select prod_name, prod_price
from products
where prod_price between 5 and 10;
// 空值检查
select cust_id from customers where cust_email is null;
// AND 操作符
select prod_id, prod_price
from produc