mysql聯合查詢時count使用的問題

2021-10-25 18:50:10 字數 1248 閱讀 2146

select  t1.id 'shopid'

, t1.shop_name 'shopname'

, t1.logo_url 'logourl'

, t1.keyword 'keyword'

, t1.seller_id 'sellerid'

,count

(t2.id)

'ordercount'

from hx_shop_info t1

left

join hx_order t2 on t2.shop_id = t1.id

where t1.entity_status =

1and t2.entity_status =

1and t2.order_status =

40and t1.shop_name like

'%九九九%'

and t1.id is

notnull

order

by t1.created_time desc

limit0,

15

這段sql如果左表沒有資料,會出現null+0的組合。

具體原因還沒找到,但是可以使用group by的方式處理,修改後sql如下:

select  t1.id 'shopid'

, t1.shop_name 'shopname'

, t1.logo_url 'logourl'

, t1.keyword 'keyword'

, t1.seller_id 'sellerid'

,count

(t2.id)

'ordercount'

from hx_shop_info t1

left

join hx_order t2 on t2.shop_id = t1.id

where t1.entity_status =

1and t2.entity_status =

1and t2.order_status =

40and t1.shop_name like

'%九九九%'

and t1.id is

notnull

-- 新增部分

group

by t1.id

order

by t1.created_time desc

limit0,

15

mysql聯合查詢

有乙個前提很重要 就是兩個表中的對應字段應該是建立聯合關係且該鍵應唯一 在查詢該聯合建的時候要指明 表.欄位 1.select from 表a,表a子表 where表a.filecode 表a子表.filecodeand表a.id in select 表a子表 id from 表a子表 where ...

MySQL聯合查詢

1.select test.name,test2.name2 from test left join test2 on test.id test2.id 2.select test.name,test2.name2 from test right join test2 on test.id test...

mysql聯合查詢

mysql聯合查詢效率較高,以下例子來說明聯合查詢 內聯 左聯 右聯 全聯 的好處 t1表結構 使用者名稱,密碼 userid int usernamevarchar 20 passwordvarchar 20 1 jack jackpwd 2 owen owenpwd t2表結構 使用者名稱,積分...