一、检索数据
1.1、检索单个列 :
SELECT prod_name FROM products;
1.2、检索多个列 :SELECT prod_id, prod_name,prod_price FROM products;
SELECT prod_id, prod_name,prod_price FROM products;
1.3、检索所有列 :SELECT * FROM products;
SELECT * FROM products;
1.4、检索不同的行(如果要输出唯一的结果就要使用,因为SELECT vend_id FROM products; 将会输出重复的结果) :
SELECT DISTINCT vend_id FROM products;
注:如果用DISTINCT关键字加上不同类型的两列,除非两列所有都不同,否则都全输出,相当于失效;
SELECT DISTINCT vend_id FROM products;
1.5、限制结果:
(1)返回前5行 :SELECT prod_name FROM products LIMIT 5;
SELECT prod_name FROM products LIMIT 5;
(2)返回第5行到其后的五行:SELECT prod_name FROM products LIMIT 5,5;
SELECT prod_name FROM products LIMIT 5,5;
二、排序检索数据
2.1、按单个排序:SELECT prod_name FROM products ORDER BY prod_name
SELECT prod_name FROM products ORDER BY prod_name;
2.2、按多个排序:SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_name, prod_price;
SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_name, prod_price;
2.3 降序排序:(1) SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC;
SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC;
(2) SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC, prod_name;
SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price DESC, prod_name;
三、过滤数据
3.1、使用WHERE子句:SELECT prod_name, prod_price FROM products WHERE prod_price = 2.50;
SELECT prod_name, prod_price FROM products WHERE prod_price = 2.50;
3.2、 使用WHERE子句操作符:
3.2.1 (1)检查单个值:SELECT prod_name, prod_price FROM products WHERE prod_name = ‘fuses‘;
SELECT prod_name, prod_price FROM products WHERE prod_name = ‘fuses‘;
(2) SELECT prod_name, prod_price FROM products WHERE prod_price < ‘10;
SELECT prod_name, prod_price FROM products WHERE prod_price < 10;
3.2.2 不匹配查询
(1)找出不是102供应商供应的货品:SELECT vend_id, prod_name FROM products WHERE vend_id <> 102;
SELECT vend_id, prod_name FROM products WHERE vend_id <> 102;
3.2.3 范围值查询
(1) 查找价格在 5 和 10 之间的货物:
SELECT prod_name, prod_price FROM products WHERE prod_price BETWEEN 5 AND 10;
SELECT prod_name, prod_price FROM products WHERE prod_price BETWEEN 5 AND 10;
3.2.4 空值查询
查找价格为null 的货物: SELECT prod_name FROM products WHERE prod_price IS NULL;
SELECT prod_name FROM products WHERE prod_price IS NULL;
四、数据过滤、
4.1