首頁 >資料庫 >mysql教程 >mysql中sql區塊的用法是什麼

mysql中sql區塊的用法是什麼

coldplay.xixi
coldplay.xixi原創
2020-11-03 11:07:122713瀏覽

mysql中sql區塊的用法:1、進行非預設的排序;2、更新替換某一個欄位中的特定字串【SELECT a.id FROM(..)】;3、求比例的sql【b.num / a.sum * 100】。

mysql中sql區塊的用法是什麼

mysql中sql區塊的用法:

1、使用非預設的排序 ,例如使用213之類的排序

可以使用以下方法

SELECT DISTINCT
pg.part_grp_id,
pg.part_grp_name,
pg.equip_category_id
FROM
cost_part_grp pg,
cost_part_kit pk,
cost_part_event pe
WHERE
pe.mdl_ver_id IN (
SELECT
s.mdl_ver_id
FROM
cost_fleet_model s
WHERE
s.fleet_id = 1002
)
AND pe.part_kit_id = pk.part_kit_id
AND pk.part_grp_id = pg.part_grp_id
ORDER BY
CASE pg.equip_category_id
WHEN 2 THEN
1
WHEN 1 THEN
2
WHEN 4 THEN
3
ELSE
4
END

2、mysql更新取代某一個欄位中的特定字串

UPDATE t_bss_employees
SET mobilephone = REPLACE (mobilephone, "2129", "0000")
WHERE
id IN (
SELECT
a.id
FROM
(
SELECT
id
FROM
t_bss_employees
WHERE
mobilephone IN (
'18121299262',
'18121299247',
'18121299206',
'18121299209'
)
) AS a
)

將mobilephone欄位中的2129字串替換為0000

使用目前表為條件更新目前表。需要在條件處再增加一個()建立一個虛擬表

3、求比例的sql

select a.style, ROUND(
            b.num / a.sum * 100,
            2
        ) as styleRate from (SELECT 
count(qspc.id) sum 
,qq.style  style
from  t_qc_security_plan_comment  qspc 
inner join t_qc_question qq
on qq.id = qspc.question_id
LEFT JOIN t_qc_address qa ON qa.id = qspc.address_id
LEFT JOIN t_system_organ  so on qa.hospital =  so.id
WHERE qq.scr_level is not NULL
and qq.type = 3  and so.tenant_code = 'zzyy'
GROUP BY style) a left join (SELECT 
count(qspc.id) num 
,qq.style  style
from  t_qc_security_plan_comment  qspc 
inner join t_qc_question qq
on qq.id = qspc.question_id
LEFT JOIN t_qc_address qa ON qa.id = qspc.address_id
LEFT JOIN t_system_organ  so on qa.hospital =  so.id
WHERE qq.scr_level is not NULL
and qq.type = 3  and so.tenant_code = 'zzyy'
and qspc.is_bad =0
GROUP BY style) b on a.style = b.style

計算邏輯是分別求總數和平均數。然後兩數相除再用round求精度

更多相關免費學習推薦:mysql教學(影片)

以上是mysql中sql區塊的用法是什麼的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn