mysql操作遇到的坑(第一版)

2022-08-29 09:15:15 字數 1580 閱讀 6280

1、當我們要統計資料表數量時,如果遇到多表查詢,會出現乙個主表對應多個子表的維度,我們會用到group by,但是不要再用統計函式去運算元據,因為統計還是會統計原資料

案例select

sum(`order`.goods_amount) as total_goods_amount,

sum(`order`.order_amount) as total_order_amount,

sum(`order`.redbag_amount) as total_redbag_amount,

sum(`order`.spread_amount) as total_spread_amount,

sum(`order`.redrain_amount) as total_redrain_amount

from

ds_order as `order`

left join ds_refundreturn as refundreturn on `order`.order_id = refundreturn.order_id

where

1 = 1

and `order`.order_state = '40'

and refundreturn.refund_state < '3'

and refundreturn.is_revoke = '0'

group by

`order`.order_id

這樣得到的資料,會把子表多個訂單id給統計上去,而分組並沒有限制到

2、可以用資料庫子查詢,最後再加乙個總查詢

select sum(a.total_goods_marketprice) from (

select

goods.goods_marketprice * ordergoods.goods_num

) as total_goods_marketprice

from

ds_order as `order`

left join ds_refundreturn as refundreturn on `order`.order_id = refundreturn.order_id

left join ds_ordergoods as ordergoods on `order`.order_id = ordergoods.order_id

left join ds_goods as goods on ordergoods.goods_id = goods.goods_id

where

1 = 1

and `order`.order_state = '40'

and refundreturn.refund_state < '3'

and refundreturn.is_revoke = '0'

group by

`order`.order_id

)a;3、mysql group concat 長度是有限制的,如果資料太多,會擷取

4、如果在不同電腦查詢值,排序值得到的資料是相同的,有可能會顯示不同順序的資料

5、mysql看or與in的條件是一樣的,但是數量大的時候,or會影響效能

親,後面還會陸續更新!

第一版Python程式

coding cp936 import os import cpickle as p class person def init self,name,age,self.name name self.age age self.def str self return name self.name age...

MVC面試第一版

mvc流程 1 使用者向伺服器傳送請求,請求被springmvc 前端控制器 dispatcherservlet 捕獲 2 dispatcherservlet對請求url進行解析,得到請求資源識別符號 uri 4 dispatcherservlet 根據獲得的handler,選擇乙個合適的handl...

HomePlay第一版demo發布

一直想做乙個能夠方便跨平台開發的應用框 基礎部分已經完成,於是將這個處於襁褓中的框架應用在win32基礎之上,花了些功夫研究了ffmpeg的使用,做了乙個demo,發布出來吸引批評與建議。框架基礎部分用c語言開發,以dll方式封裝了核心功能以及對ffmpeg的使用部分。出於簡單考慮,外部採用mfc包...