1.查询语法规则
select [ all | distinct] <目标列表达式> [别名] [ , <目标列表达式> [别名] ] . . .
from <表名或视图名> [别名]
[where <条件表达式>]
[group by <列名1>]
[having <条件表达式> ]
[order by <列名2>[ ASC| DESC ]]
[limit 字句];
其中[ ]内的内容是可选的。
2.查询所有字段
查询所有字段是指查询表中的所有字段的数据,有两种方式:一种是列出表中的所有字段,另一种是使用通配符*来查询。[例]
SELECT id, category_id, spu_id, sn, NAME, keyword,picture, tips, description, content, price, stock, score, is_on_sale, is_del,is_free_shipping, sell_count, comment_count, on_sale_time,create_time,update_time FROM mydb.sh_goods;
SELECT * FROM mydb.sh_goods;#简单
SELECT NAME, id, category_id, spu_id, sn, keyword,picture, tips, description, content, price, stock, score, is_on_sale, is_del,is_free_shipping, sell_count, comment_count, on_sale_time,create_time,update_time FROM mydb.sh_goods;#可调整顺序
3.查询指定字段(投影)
SELECT id, NAME, keyword, content, price, stock,score, on_sale_time,create_time, update_time FROM mydb.sh_goods;
4.不去重与去重查询数据。
不去重查询数据
SELECT keyword FROM mydB.sh_goods;
去重查询数据
SELECT DISTINCT keyword FROM mydB. sh_goods;
5.为字段取别名。
SELECT id AS 商品id, NAME 名称, keyword 关键词, content 详情, price 价格, stock 库存, score 评分, on_sale_time 上架时间,create_time 创建时间, update_time 更新时间 FROM mydb.sh_goods;
6.增加通过计算得到临时字段
SELECT number AS 学号, NAME 姓名, yw AS 语文,sx AS 数学,yy AS 英语,wl AS 物理 FROM mydb.cj_table;
SELECT numberAS 学号, NAME 姓名, yw 语文,sx 数学,yy 英语,wl 物理,yw+sx+yy+wl AS 总分,ROUND((yw+sx+yy+wl)/4,1) AS 均分 FROM mydb.cj_table;