1、检索数据
1.1、检索单个列:SELECT prod_name FROM products;
1.2、检索多个列:SELECT prod_id, prod_name, prod_price FROM products;
1.3、检索所有列:SELECT * FROM products;
1.4、检索不同的行:SELECT DISTINCT vend_id FROM products;
1.5、限制结果:SELECT prod_name FROM products LIMIT 5;
SELECT prod_name FROM products LIMIT 3,4; //表示返回从行3开始的4行
注意,检索出来的第一行是行0,而不是行1。
2、检索排序数据
2.1、排序数据:SELECT prod_name FROM products ORDER BY prod_name;
2.2、按多个列排序:SELECT prod_id, prod_price, prod_name FROM products ORDER BY prod_price, prod_name;
2.3、指定排序方向: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, prod_name ASC;
2.4、找出最高或最低的值:SELECT prod_price FROM products ORDER BY prod_price DESC limit 1;
3、过滤数据
3.1、使用where子句:SELECT prod_name, prod_price FROM products WHERE prod_price = 2.50;
3.2、where子句操作符:=, <>, !=, <, <=, >, >=, BETWEEN
3.2.1、检查单个值:SELECT prod_name, prod_price FROM products WHERE prod_name=‘fuses’;
3.2.2、不匹配检查:SELECT vend_id, prod_name FROM products WHERE vend_id <> 1003;
3.2.3、范围值检查:SELECT prod_name, prod_price FROM products WHERE prod_price BETWEEN 5 AND 10;
3.2.4、空值检查:SELECT prod_name FROM products WHERE prod_price IS NULL;
4、数据过滤
4.1、组合where子句
4.1.1、AND操作符:SELECT prod_id, prod_price, prod_name FROM products where vend_id = 1003 AND prod_price <= 10;
4.1.2、OR操作符:SELECT prod_name, prod_price FROM products WHERE vend_id=1002 OR vend_id=1003;
4.1.3、计算次序:SELECT prod_name, prod_price FROM products WHERE (vend_id = 1002 OR vend_id=1003) AND prod_price >= 10;
4.2、IN操作符:SELECT prod_name, prod_price FROM products WHERE vend_id IN (1002, 1003) ORDER BY prod_name;
4.3、NOT操作符:SELECT prod_name, prod_price FROM products WHERE vend_id NOT IN (1002, 1003) ORDER BY prod_name;
5、用通配符进行过滤
5.1、LIKE操作符
5.1.1、百分号(%)通配符:表示任何字符出现任何次数, SELECT prod_id, prod_name FROM products WHERE prod_name LIKE ‘jet%’;
5.1.2、下划线(_)通配符:匹配单个字符,SELECT prod_id, prod_name FROM products WHERE prod_name LIKE ‘_ ton anvil’;
6、用正则表达式进行搜索
6.1、使用MySQL正则表达式
6.1.1、基本字符匹配:SELECT prod_name FROM products WHERE prod_name REGEXP ‘1000’ ORDER BY prod_name;
SELECT prod_name FROM products WHERE prod_name REGEXP ‘.000’ ORDER BY prod_name;(LIKE匹配整个列,REGEXP匹配列中值)
6.1.2、进行OR匹配:SELECT prod_name FROM products WHERE prod_name REGEXP ‘1000|2000’ ORDER BY prod_name;
6.1.3、匹配几个字符之一:SELECT prod_name FROM products WHERE prod_name REGEXP ‘[123] Ton’ ORDER BY prod_name;
6.1.4、匹配范围:SELECT prod_name FROM products WHERE prod_name REGEXP ‘[1-5] Ton’ ORDER BY prod_name;
6.1.5、匹配特俗字符:SELECT prod_name FROM products WHERE prod_name REGEXP ‘\\.’ ORDER BY prod_name;
7、创建计算字段
7.1、拼接字段:SELECT Concat(vend_name, ‘(’, vend_country, ‘)’) FROM vendors ORDER BY vend_name;
7.2、使用别名:SELECT Concat(RTrim(vend_name), ‘(‘, RTrim(vend_country), ‘)’) AS vend_title FROM vendors ORDER BY vend_name;
7.3、执行算数计算:SELECT prod_id, quantity, item_price, quantity*item_price AS expanded_price FROM order items WHERE order_num=20005;
8、使用数据处理函数
8.1、文本处理函数:SELECT vend_name, Upper(vend_name) AS vend_name_upcase FROM vendors ORDER BY vend_name;
常用文本处理函数:Left(), Length(), Locate(), Lower(), LTrim(), Right(), RTrim(), Soundex(), SubString(), Upper
8.2、日期和时间处理函数:SELECT cust_id, order_num FROM orders WHERE YEAR(order_date) = 2005 AND Month(order_date) = 9;
8.3、数值处理函数:abs(), cos(), exp() 等等。
9、汇总数据
9.1、AVG()函数:SELECT AVG(prod_price) AS avg_price FROM products;
9.2、COUNT()函数:SELECT COUNT(*) AS num_cust FROM customers;
9.3、MAX()函数:SELECT MAX(prod_price) AS max_price FROM products;
9.4、MIN()函数:SELECT MIN(prod_price) AS min_price FROM products;
9.5、SUM()函数:SELECT SUM(quantity) AS items_ordered FROM orderitems WHERE order_num = 2005;
9.6、聚集不同值:SELECT MIN(DISTINCT prod_price) AS min_price FROM products;
10、分组数据
10.2、创建分组:SELECT vend_id, COUNT(*) AS num_prods FROM products GROUP BY vend_id;
10.3、过滤分组:SELECT cust_id, COUNT(*) AS orders FROM orders GROUP BY cust_id Having COUNT(*) >= 2;
SELECT vend_id, COUNT(*) AS num_prods FROM products WHERE prod_price >= 10 GROUP BY vend_id;
10.4、分组和排序:SELECT order_num, SUM(quantity*item_price) AS ordertotal FROM orderitems GROUP BY order_num HAVING SUM(quantity*item_price) >= 50 ORDER BY ordertotal ;
10.5、SELECT子句顺序:
SELECT, FROM, WHERE, GROUP BY, HAVING, ORDER BY, LIMIT
11、使用子查询
SELECT cust_id FROM orders WHERE order_num IN (SELECT order_num FROM order items WHERE prod_id=’TNT2’);
SELECT cust_name, cust_state, (SELECT COUNT(*) FROM orders WHERE orders.cust_id = customers.cust_id) AS orders FROM customers ORDER BY dust_name;
12、联结表
SELECT vend_name, prod_name, prod_price FROM vendors, products WHERE vendors.vend_id = products.vend_id ORDER BY vend_name, prod_name;
SELECT vend_name, prod_name, prod_price FROM vendors INNER JOIN products ON vendors.vend_id = products.vend_id ORDER BY vend_name, prod_name;
SELECT vend_name, prod_name, prod_price FROM vendors, products, orderitems WHERE vendors.vend_id = products.vend_id AND orderitems.prod_id=produts.prod_id AND order_num=20005;
13、创建高级联结
13.1、自联结:SELECT p1.prod_id, p1.prod_name FROM products AS p1, products AS p2 WHERE p1.vend_id=p2.vend_id AND p2.prod_id=‘DTNTR‘;
13.2、外联结:SELECT customers.cust_id, orders.order_num FROM orders LEFT OUTER JOIN orders ON customers.cust_id=orders.cust_id;
13.3、使用带聚集函数的联结:SELECT customers.cust_name, customers.cust_id, COUNT(orders.order_num) AS num_ord FROM customers INNER JOIN orders ON customers.cust_id = orders.cust_id GROUP BY customers.cust_id;
14、插入数据
INSERT INTO customers(cuts_name, cuts_contact) VALUES (‘ppp’, ‘ppp‘);
INSERT INTO customers(cuts_name, cuts_contact) VALUES (‘ppp’, ‘ppp‘)(‘ttt‘, ‘ttt‘)(‘qqq‘, ‘qqq‘);
15、更新和删除数据
UPDATE customers SET cuts_name=’ttt’, cuts_email=‘eee@gmial.com’ WHERE cuts_id = 10005;
DELETE FROM customers WHERE cuts_id=10006;
原文:http://www.cnblogs.com/timlearn/p/4039099.html