明輝手游網(wǎng)中心:是一個(gè)免費(fèi)提供流行視頻軟件教程、在線學(xué)習(xí)分享的學(xué)習(xí)平臺(tái)!

mysql語(yǔ)句基礎(chǔ)詳細(xì)說(shuō)明

[摘要]本文主要和大家分享mysql語(yǔ)句入門詳解,專門和大家一步一步詳解mysql語(yǔ)句,希望能幫助到大家。Select,Left Join的用法,及模糊查詢的實(shí)現(xiàn):LEFT JOIN 關(guān)鍵字會(huì)從左表 (ta...

本文主要和大家分享mysql語(yǔ)句入門詳解,專門和大家一步一步詳解mysql語(yǔ)句,希望能幫助到大家。

Select,Left Join的用法,及模糊查詢的實(shí)現(xiàn):

LEFT JOIN 關(guān)鍵字會(huì)從左表 (table_name1) 那里返回所有的行,即使在右表 (table_name2) 中沒(méi)有匹配的行。
(業(yè)務(wù)需求:從一張表查詢出來(lái)的數(shù)據(jù)要關(guān)聯(lián)到另一個(gè)表,并實(shí)現(xiàn)模糊查詢)

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的用法

(業(yè)務(wù)需求:當(dāng)某個(gè)字段有傳進(jìn)來(lái)時(shí)就更新,沒(méi)有傳進(jìn)來(lái)時(shí)忽略,這樣可以根據(jù)你的需求來(lái)選擇是否插入某個(gè)字段)

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

Delete,In的用法:(IN 操作符允許我們?cè)赪HERE子句中規(guī)定多個(gè)值)

下面的SQL語(yǔ)句可以刪除多條符合條件的數(shù)據(jù)
(業(yè)務(wù)需求: 進(jìn)行刪除操作時(shí),創(chuàng)建數(shù)據(jù)的人若是操作數(shù)據(jù)的人,則刪除,否則更新?tīng)顟B(tài))
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的用法

(業(yè)務(wù)需求:新增一條新的數(shù)據(jù))

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

(業(yè)務(wù)需求:每新增一條數(shù)據(jù),某一字段在原有的數(shù)據(jù)的最大值基礎(chǔ)上+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`;

(業(yè)務(wù)需求:從其他表中新增數(shù)據(jù)到當(dāng)前表)

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

相關(guān)推薦:

實(shí)現(xiàn)MySQL語(yǔ)句加鎖的方法

用mysql語(yǔ)句創(chuàng)建數(shù)據(jù)表詳細(xì)教程

對(duì)mysql語(yǔ)句的性能分析與優(yōu)化

以上就是mysql語(yǔ)句入門詳解的詳細(xì)內(nèi)容,更多請(qǐng)關(guān)注php中文網(wǎng)其它相關(guān)文章!


學(xué)習(xí)教程快速掌握從入門到精通的SQL知識(shí)。