1、sql语句书写顺序
select===>from===>where===>group by===>having===>order by
2、sql语句解析顺序
from===>where===>group by===> having===>select===>order by
3、数据操作语言 (DML) 和 数据定义语言 (DDL)
DML 部分:
SELECT - 从数据库表中获取数据
UPDATE - 更新数据库表中的数据
DELETE - 从数据库表中删除数据
INSERT INTO - 向数据库表中插入数据
DDL 语句:
CREATE DATABASE - 创建新数据库
ALTER DATABASE - 修改数据库
CREATE TABLE - 创建新表
ALTER TABLE - 变更(改变)数据库表
DROP TABLE - 删除表
CREATE INDEX - 创建索引(搜索键)
DROP INDEX - 删除索引
4、sql一些常用查询
-- 根据商品分类id查询品牌列表
select * from tb_brand tb,tb_category_brand cb where tb.`id`= cb.`brand_id` and cb.`category_id`=76
-- 查询中间表数据下的另一张表数据
select m.* from tb_role_module r inner join tb_module m on m.`module_id` = r.`module_id` where r.`role_id`=1
-- 查询所有的cid进行序列化排序
@Select("select * from tab_category order by cid")
-- 更新线路的数量
@Update("update tab_route set count = count + 1 where rid=#{rid}")
-- 查询一个用户收藏的数量
@Select("select count(*) from tab_favorite where uid=#{uid}")
-- 查询一个用户收藏的一页的数据(排序)
@Select("select * from tab_route r inner join tab_favorite f on r.rid = f.rid where uid=#{uid} order by f.date desc limit #{begin},#{size}")
-- 添加1条收藏的记录
@Insert("insert into tab_favorite (rid, `date`, uid) values (#{route.rid}, now(), #{user.uid})")
-- 查询某个分类下,线路一页的数据
@Select("select * from tab_route where cid=#{cid} and rname like '%${rname}%' limit #{begin},#{size}")
-- 得到某个分类下线程总记录数
@Select("select count(*) from tab_route where cid=#{cid} and rname like '%${rname}%'")
-- 查询6条的方法
@Select("select * from tab_route where cid=#{cid} order by count desc limit 0,6")
<!--定义一个SQL代码段-->
<sql id="conditionSql">
<where>
<if test="condition.rname!=null and condition.rname!=''">
rname like '%${condition.rname}%'
</if>
<if test="condition.startPrice!=null and condition.startPrice!=''">
and price >= #{condition.startPrice}
</if>
<if test="condition.endPrice!=null and condition.endPrice!=''">
and price <= #{condition.endPrice}
</if>
</where>
</sql>
-- 得到所有收藏的总数
select count(*) from tab_route <include refid="conditionSql"/>
-- 查询收藏排行榜的进行降序排序,每页显示8页
select * from tab_route
<include refid="conditionSql"/>
order by count desc limit #{begin},#{size}