旗下导航:搜·么
当前位置:网站首页 > MySQL教程 > 正文

mysql语句入门详解【MySQL教程】,mysql,语句,入门

作者:搜教程发布时间:2019-12-01分类:MySQL教程浏览:97评论:0


导读:本文主要和人人分享mysql语句入门详解,特地和人人一步一步详解mysql语句,希望能协助到人人。Select,LeftJoin的用法,及隐约查询的完成:LEFTJOI...

本文主要和人人分享mysql语句入门详解,特地和人人一步一步详解mysql语句,希望能协助到人人。

Select,Left Join的用法,及隐约查询的完成:

LEFT JOIN 关键字会从左表 (table_name1) 那边返回一切的行,纵然在右表 (table_name2) 中没有婚配的行。
(营业需求:从一张表查询出来的数据要关联到另一个表,并完成隐约查询)

const sql = `select cei.intentionID,cei.engineerName,cei.nickname,cei.headImgUrl,
ifnull(cmw.headImgUrl,'') inviteImgUrl,ifnull(cmw.nickname,'') inviteNickName
from cm_engineer_intentions cei
left join cm_member_wechats cmw on cmw.referralCode=cei.inviteCode
where (:isGrant=-1 or isGrant=:isGrant)
and (:intentionStatus=-1 or intentionStatus=:intentionStatus)
and (:searchKey='' or concat(engineerMobile,engineerName) like concat('%',:searchKey,'%'))
order by intentionStatus asc,createTime desc`;

Update Set的用法

(营业需求:当某个字段有传进来时就更新,没有传进来时疏忽,如许能够依据你的需求来挑选是不是插进去某个字段)

const contactRemarkSql = isUpdateRemark ? ',contactRemark=:contactRemark' : '';
const sql = `update cm_engineer_intentions
set intentionStatus=:intentionStatus ${contactRemarkSql}
where intentionID=:intentionID`;

Delete,In的用法:(IN 操纵符许可我们在WHERE子句中划定多个值)

下面的SQL语句能够删除多条相符前提的数据
(营业需求: 举行删除操纵时,建立数据的人如果操纵数据的人,则删除,不然更新状况)
const deleteSql = delete from cm_fast_order_goods where orderGoodsID in (${deleteData.deleteIDs}) and createUser=:userName;
const updateSql = update cm_fast_order_goods set status=0 where orderGoodsID in (${deleteData.deleteIDs}) and createUser!=:userName;

Insert的用法

(营业需求:新增一条新的数据)

const sqlOrderEngineer = `insert into cm_order_engineers
(orderEngineerID,orderGoodsID,orderID,engineerID,engineerName,createUser,createTime)
values
(:orderEngineerID,:orderGoodsID,:orderID,:engineerID,:engineerName,:createUser,:createTime)

(营业需求:每新增一条数据,某一字段在原有的数据的最大值基础上+1)

const sql = `insert into cm_engineers
(engineerCode,loginName,password,engineerName,sex,birthDate,status,createUser,createTime)
select
ifnull(max(engineerCode),0)+1,:loginName,:password,:engineerName,:sex,:birthDate,
:status,:createUser,now()
from cm_engineers`;

(营业需求:从其他表中新增数据到当前表)

insert into cm_fast_order_goods (orderGoodsID,orderID,goodsID,goodsCode,goodsPicUrl,createUser,createTime)
select :orderGoodsID,:orderID,:goodsID,goodsCode,iconUrl,:createUser,now()
from cm_service_goods
where goodsID=:goodsID and status=1

相干引荐:

完成MySQL语句加锁的要领

用mysql语句建立数据表细致教程

对mysql语句的机能剖析与优化

以上就是mysql语句入门详解的细致内容,更多请关注ki4网别的相干文章!

标签:mysql语句入门


欢迎 发表评论: