救命!!!帮帮忙行吗!!还是SQL统计问题1(15分)

  • 主题发起人 主题发起人 yyjqtww
  • 开始时间 开始时间
Y

yyjqtww

Unregistered / Unconfirmed
GUEST, unregistred user!
我用如下语句:
SELECT s.isbn, SUM(s.linqu_total) + SUM(t.linqu_total) + SUM(l.buy_total) AS totalsum
FROM ystudentlinshu AS s, yteacherlinshu AS t, ylinshoutab AS l
WHERE (s.isbn = t.isbn) AND (s.isbn = l.isbn)
GROUP BY s.isbn
ORDER BY s.isbn
只统计出三表中具有相同字段值的记录,可是我还想让三张表中不相同的记录也显示出来,即不相同的既然不统计,但也显示它,应该如何实现呢??谢谢!
如表别名s.t.l中有
s t l
isbn total isbn total isbn total
1-1 10 1-1 10 1-1 10
2-2 10 2-1 10 2-2 10
3-3 10 3-1 10 1-1 15
想得到如下结果
isbn totalsum
1-1 45
2-1 10
2-2 20
3-1 10
3-3 10
 
SUM 是统计 函数,这样做行不通的,
建议作个 汇总统计的 表吧
 
我用如下语句:
SELECT s.isbn, SUM(s.linqu_total) + SUM(t.linqu_total) + SUM(l.buy_total) AS totalsum
FROM ystudentlinshu AS s, yteacherlinshu AS t, ylinshoutab AS l
WHERE (s.isbn = t.isbn) AND (s.isbn = l.isbn)
GROUP BY s.isbn
UNION
select isbn, linqu_total, 0, 0 from ystudentlinshu where isbn not in (select isbn from yteacherlinshu union select isbn from ylinshoutab)
UNION
select isbn, 0, linqu_total, 0 from yteacherlinshu where isbn not in (select isbn from ystudentlinshu union select isbn from ylinshutab)
UNION
select isbn, 0, 0, buy_total from ylinshutab where isbn not in (select isbn from ystudentlinshu union select isbn from yteacherlinshu)
 
Pearl的语句运行时有错误!
 
嘻嘻, 写错了一点点嘛
SELECT s.isbn, SUM(s.linqu_total) + SUM(t.linqu_total) + SUM(l.buy_total) AS totalsum
FROM ystudentlinshu AS s, yteacherlinshu AS t, ylinshoutab AS l
WHERE (s.isbn = t.isbn) AND (s.isbn = l.isbn)
GROUP BY s.isbn
UNION
select isbn, linqu_total from ystudentlinshu where isbn not in (select isbn from yteacherlinshu union select isbn from ylinshoutab)
UNION
select isbn, linqu_total from yteacherlinshu where isbn not in (select isbn from ystudentlinshu union select isbn from ylinshutab)
UNION
select isbn, buy_total from ylinshutab where isbn not in (select isbn from ystudentlinshu union select isbn from yteacherlinshu)
 
后退
顶部