作者bobju (宝贝猪)
看板Database
标题Re: [SQL ] 请问要怎麽做两个资料表的查询
时间Thu May 7 00:13:15 2009
用 left join 把关联资料表串起来, 并下where constraint筛选出符合条件的资料.
例如要查 3 月份兄弟象每个球员的"每笔单场分数":
select b.*,c.*
from c
left join b on b.bid=c.bid
left join a on a.aid=b.aid
where a.team='兄弟象' and ('0301' <= c.day) and (c.day <= '0331')
另外, 还可用 group by 做统计, 计算出每个球员之单月累计总分
select c.bid,sum(c.point)
from c
left join b on b.bid=c.bid
left join a on a.aid=b.aid
where a.team='兄弟象' and ('0301' <= c.day) and (c.day <= '0331')
group by c.bid
这SQL在phpmyadmin下测试过了, ok.
※ 引述《lottemarines (一平)》之铭言:
: a资料表
: aid team
: 1 兄弟象
: 2 味全龙
: 3 统一狮
: b资料表
: bid player aid
: 1 王大毛 1
: 2 李小虎 1
: 3 陈自强 3
: 4 吴小弟 1
: c资料表
: cid bid day point
: 1 1 0301 10
: 2 1 0302 20
: 3 2 0302 15
: 4 3 0303 -10
: 5 4 0304 -15
: 要怎麽查某一段时间内,兄弟象球员的分数呢?是否跟left right有关呢?
: 感谢
--
※ 发信站: 批踢踢实业坊(ptt.cc)
◆ From: 59.104.191.26