Appearance
常用SQL写法
1.连接查询 select xxx from tableA join tableB on A.column= B.column where 筛选条件主要用于单表数据不全时需要从其他表查数据结果值当且仅当两者的关联字段相等时才会查到 E8:出库表只有物料编号需要查物料名、规格则用出库表关联物料表 2.左连接查询 select xxx from tableA left join tableB on A.column= B.column where 筛选条件使用 Leftjoin 查询时无论A表(等式左边)的值有没有关联到右边的表 都会查到左边的数据 主要用于非强关联即关联表的数据可有可无注意当需要强关联时不可使用否则会导致数据不对E8:查询客户及客户送货地址时可能存在没有地址的客户但是没有地址的客户也需要列出来这时候就使用客户表leftjoin地址表做查询
3.union al 合并查询结果集注意:使用 unional 时要求查询字段数量和顺序完全一致 否则会出现报错或数据错乱等问题 写法 select a,b from tableA union all select a,b from tableAunionall 常用于进销存、小计等查询 进销存案例:
美化:
简化: 进销存查询 Select beg+in-out as end from (select beg,0 asin,0as out from 仓库记录表union all select0as beg,in,0 as outfrom 仓库记录表 union all select0as beg,0 asin,out from 仓库记录表 ) as hbb 最后将合并表的期初+入库-出库即可得到期末
4.关联更新 当页面上要执行 update语句 但update 的值不是输入的 是从另一张表取到的 就需要用的这种关联更新 写法:update tableA as a join tableB as b on a.column = b.column set a.xxx=b.xxx where 筛选条件 Eg:当改批任务单执行到某个动作时 需要取改批计划单的数据回写时 即可用到这种 updatejoin on 的写法