使用完全限定的表名,products是表的名字,limit 3,4表达的意思是从行3开始的4行(第一行是行0)
SELECT products.prod_name FROM products LIMIT 3,4 limit 1 就只返回一行
对prod_name列以字母排序(可以多个列)
SELECT products.prod_name FROM products ORDER BY prod_name SELECT * FROM products ORDER BY prod_price,prod_name
注意desc/asc只对第一个列名有效,效果如下:
- asc是升序
- desc是降序
范围值检查
SELECT * FROM `products` WHERE `prod_price` BETWEEN 5 AND 10
and操作符、or操作符、in操作符
SELECT * FROM `products` WHERE `vend_id` = 1003 AND `prod_price` <=10 SELECT * FROM `products` WHERE `vend_id` = 1003 or `prod_price` <=10 SELECT * FROM `products` WHERE `vend_id` IN (1002,1003)
百分号通配符
SELECT * FROM `products` WHERE `prod_name` LIKE 'jet%' # 检索所有jet开头的词,不管多少字符 SELECT * FROM `products` WHERE `prod_name` LIKE '%anvil%' #使用两个%,只要文本中含有这部分,就满足条件
聚集函数
avg() #返回某列的平均值 count() #返回某列的行数 max() #返回某列的最大值 min() #返回某列的最小值 sum() #返回某列值的和
SELECT AVG( `products`.`prod_price`) AS avg_price FROM `products` #这个语句的返回值是avg_price,只是一个别名 SELECT COUNT( *) AS num_count FROM `customers` #统计有几个客户有电子邮箱 SELECT COUNT( `cust_email`) AS num_count FROM `customers` #返回最高价格物品的价格、最低价格物品的价格(忽略列值为null的行) SELECT MAX( `prod_price`) AS maxx FROM `products` SELECT MIN( `prod_price`) AS maxx FROM `products` #返回订单中所有物品数量之和 SELECT SUM( `quantity`) as total_ans FROM `orderitems` WHERE `order_num` = 20005