一.普通的检索数据
1.1检索某一列
select prod_id,prod_name,prod_price from products;
1.2检索所有列
select * from products;
1.3去重查询
select from ven_id from products;
select distinct vend_id from products;
1.4限制查询
select prod_name from products limit 5;
注:limit 5 表示 查询结果不超过5行
select prod_name from products limit 5,5;
注:从第五行开始,查询5行
二.排序数据
select prod_name from products order by prod_name;
select prod_id,prod_price,prod_name from products order by prod_price,prod_name;
select prod_id,prod_price,prod_name from products order by prod_price DESC;
注:desc关键字只应用于直接位于前面定位列名,与desc相反的关键字是asc,但asc没什么用,因为默认就是升序的。
三.过滤数据
select prod_name,prod_price from products where prod_price = 2.50;
注:使用where子句时,应该让order by位于where之后。 下表是MySQL支持的条件操作符:
3.1范围检查
select prod_name, prod_price from products where pro_price between 5 and 10;
3.2 通配符过滤
最常使用的通配符是百分号(%)。在搜索串中,%表示任何字符出现任意次数。
select pro_id, prod_name from products where prod_name like 'jet%';