SELECT prod_name FROM products;
SELECT prod_id,prod_name,prod_price
FROM products;
SELECT * FROM products;
SELECT DISTINCT vend_id From products;
SELECT prod_name FROM products LIMIT 5; #显示五行结果
SELECT prod_name From products
ORDER BY prod_name; #升序(从小到大)
首先按价格,然后按名称排序
SELECT prod_name,prod_price,prod_name
FROM products
ORDER BY prod_price,prod_name;
SELECT prod_id FROM products
ORDER BY prod_id DESC; # 降序(从大到小)
SELECT prod_price FROM products
ORDER BY prod_price DESC
LIMIT 1;
SELECT prod_name,prod_price
FROM products
WHERE prod_price=2.50; # 寻找价格位2.50的物品
SELECT prod_id,prod_price,prod_name
FROM products
WEHRE vend_id=1003 AND prod_price<=10; #AND操作符,两个条件同时满足才满足
SELECT prod_id,prod_price,prod_name
FROM products
WEHRE vend_id=1003 OR vend_id=1002; #OR操作符,两个条件满足一个即可。
AND运算符优先级高于OR
SELECT prod_name,prod_price
FROM products
WHERE vend_id IN(1002,1003) #IN操作符,属于括号里面的即会出现。
ORDER BY prod_name;
SELECT prod_name,prod_price
FROM products
WHERE vend_id NOT IN(1002,1003) #NOT操作符,取相反结果
%表示任意字符出现任意次
SELECT prod_id,prod_name FROM products
WHERE prod_name LIKE 'jet%' #搜索所有name为jet的内容
SELECT prod_id,prod_name FROM products
WHERE prod_name LIKE '%anvil%' #搜索所有name包含anvil的内容
_表示任意符号出现一次
SELECT prod_id,prod_name FROM product
WHERE prod_name LIKE '_ ton anvil'; #搜索出第一个字符任意,后面位ton anvil的内容
SELECT prod_name FROM products
WHERE prod_name REGEXP '1000'; #name搜索出包含1000的内容
SELECT prod_name FROM products
WHERE prod_name REGEXP '1000|2000'; # 匹配到name包含1000或2000的内容
SELECT prod_name FROM products
WHERE prod_name REGEXP '[123] TON'; # 匹配到name包含1 TON、2TON、3TON的内容
SELECT prod_name FROM products
WHERE prod_name REGEXP'[1-5] ton' # 匹配到name包含1 TON、2TON……5TON的内容
SELECT Concat(vend_name,' (',vend_country,')')
FROM vendors;
# 把vend_name和vend_country连接起来
# EG:vend_name='ACME' , vend_country='USA'
#OUTPUT: ACME (USA)
SELECT prod_id,quantity,item_price,
quantity*item_prics as expanded_price # 可以把乘积结果显示到表中,并且列名可以用别名
FROM orderitems WHERE order_name=20005;
SELECT vend_name,Upper(vend_name) AS vend_name_upase
FROM vendors; # Upper函数,将字母转化为大写
SELECT cust_id,order_num
FROM order
WHERE Data(order_data) BETWEEN '2005-09-01' AND '2005-09-30';
# 用data函数可以进行日期处理
SELECT AVG(prod_price) AS avg_price FROM products;
# 求出price的平均数。
SELECT cout(*) AS num_cust FROM customers;
# 求出顾客总数。
SELECT MAX(prod_price) AS max_price FROM produces;
求出price的最大值。
SELECT AVG(DISTINCT prod_price) AS agv_price
FROM products WHERE vend_id=1003; #去重后的平均值。
SELECT COUNT(*) AS num_items,
MIN(prod_price) AS price_min,
MAX(prod_price) AS price_max,
AVG(prod_price) AS price_avg
FROM produces; # 把这些函数结果组合起来
SELECT vend_id,COUNT(*) AS num_prods
FROM products
GROUP BY vend_id;
# 先对vend_id进行分组求出每组的个数后进行显示。
SELECT vend_id,COUNT(*) AS num_prods
FROM products
GROUP BY vend_id
HAVING COUNT(*)>=2; #对显示的组加了判断条件
SELECT vend_id,COUNT(*) AS num_prods
FROM products WHERE prod_price>=10 # 进行条件赛选后分组,然后再次进行赛选
GROUP BY vend_id
HAVING COUNT(*)>=2; #对显示的组加了判断条件
#同时用WEHRE和HAVING赛选
SELECT order_num.SUM(quantity*item_price) AS ordertotal
FROM orderitems
ORDER BY ordertotal; # 分组后进行排序
SELECT cust_id
FROM orders
WHERE order_num IN(SELECT order_num
FROM orderitems
WHERE prod_id='TNT2');
#先对orderitems表中检测出id为TNT2的数据的num,在从order中检索出num属于那个的id
select cust_name,
cust_state,
(select count(*)
from orders where orders.cust_id=customers.cust_id)
as orders
from customers;
# 将结果作为一个字段显示。
+----------------+---------------+----------+
| cust_name | cust_state | orders |
+----------------+---------------+----------+
| Coyote Inc. | MI | 2 |
| Mouse Hos | OH | 0 |
| Wascals | IN | 1 |
| Yosemite Place | AZ | 1 |
| E Fudd | IL | 1 |
| Pep E. laPew | CA | 0 |
| M.Martian | NY | 0 |
+----------------+------------+--------+
SQL最强大的功能之一就是能在数据检索检查的执行中联结(jion)表。
SELECT vend_name,prod_name,prod_price
FROM vendors,products
WHERE vendors.vend_id=products.vend_id;
# 将两个表通过笛卡尔积的方式联结起来,然后进行赛选。
SELECT vend_name,prod_name,prod_price
FROM vendors INNER JOIN products
ON vendors.vend_id=products.vend_id;
# 将两个表进行内部联结(其实等价于笛卡尔积)后进行赛选
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';
# 两个products表进行笛卡尔积联结后进行筛选.
SELECT customers.cust_id,orders.order_num
FROM customers LEFT OUTER JOIN orders
ON customers.cust_id=orders.cust_id;
# 检索所有客户的订单,没有订单的输出NULL