作者JGC18 (JGC)
看板Database
標題Re: [SQL ] 兩張資料表合成一張
時間Sat May 17 18:28:38 2014
※ 引述《JGC18 (JGC)》之銘言:
: (針對 SQL 語言的問題,用這個標題。請用 Ctrl+Y 砍掉這行)
: 資料庫名稱:
: MS SQL Server
: 資料庫版本:
: 2008 R2
: 內容/問題描述:
: 大家好,小弟目前碰到要把兩張資料表合成一張顯示
: table_A
: 欄位有
: store_id, data, enable
: H1 xxx.. 1
: H1 yyy.. 1
: H3 zzz.. 0
: H4 xyv.. 1
: -----------------------------
: tabke_B
: store_id, store_name
: H1 ABC
: H2 DEF
: H3 GHI
: H4 JKL
: -----------------------------
: 目前想把他合成一張表(只抓出table A中 enable=1的資料)如下
: store_id store_name, count
: H1 ABC 2
: H4 JKL 1
: 我會算出coumt
: select count(*),store_id from table_A where eable=1 group by store_id
: 但要怎麼把table_B的store_name加上去呢
: 感謝各位解答,跟指點方向
謝謝前輩回應
我使用了join的語法
select table_A.store_id,table_B.store_name from table_A left join table_B on(table_A.store_id=table_B.store_id) where store_A.enable=1
是可以把store_id,跟store_name對起來
但是如果要把count加進去就會出錯
select count(*),table_A.store_id,table_B.store_name from table_A left join table_B on
(table_A.store_id=table_B.store_id) where store_A.enable=1 group by table_A.store_id
column 'store_B.store_name' is invalid in the select list
because it is not contained in either an aggregate function or the group by clause
目前不太知道要怎麼去把這兩個合併在一起
謝謝指教
--
※ 發信站: 批踢踢實業坊(ptt.cc), 來自: 175.182.110.8
※ 文章網址: http://www.ptt.cc/bbs/Database/M.1400322521.A.758.html
推 scpisces:執行的錯誤訊息都貼出來了...... 05/17 20:24
→ scpisces:修正group by即可 05/17 20:27