作者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://webptt.com/cn.aspx?n=bbs/Database/M.1400322521.A.758.html
1F:推 scpisces:执行的错误讯息都贴出来了...... 05/17 20:24
2F:→ scpisces:修正group by即可 05/17 20:27