如何从具有不同结构的多个表中总和?

发布于 2025-01-30 19:52:26 字数 1354 浏览 6 评论 0 原文

嗨,我正在尝试通过使用SQL查询来总结“喜欢”& 量

“不喜欢”的

奖励的“数量”和减去 作为我的..

这是我的第一张桌子“文章”

用户 bob 名喜欢
3 -1

这是我的第二个表“奖项”奖励“

用户
bob 3

在这种情况下,它应该输出5 ,但我不能让它上班,

 SELECT SUM(rep) AS totalRep FROM 
  (
    SELECT `likes` FROM `articles` UNION ALL
    SELECT `dislikes` FROM `articles` UNION ALL
    SELECT `amount` FROM `awards` UNION ALL
  ) GROUP BY `username`

我也尝试过这样的..

SELECT SUM(rep) AS totalRep FROM 
  (
    SELECT SUM(likes) FROM `articles` UNION ALL
    SELECT SUM(dislikes) FROM `articles` UNION ALL
    SELECT SUM(amount) FROM `awards` UNION ALL
  ) GROUP BY `username`

和这样的..

SELECT rep SUM(totrep) AS totrep FROM 
(
  SELECT likes,dislikes FROM `articles`
  UNION ALL
  SELECT amount FROM `awards`
) GROUP BY `username`

和..

SELECT SUM(likes,dislikes,amount) AS totrep
  FROM (
        SELECT likes,dislikes FROM `articles`
        UNION ALL
        SELECT amount FROM `awards`
       )  GROUP BY `username`

Hi I am attempting to create a "Reputation" for a user's profile by using SQL query to sum up the 'likes' & 'amount' of awards and subtracting the amount of 'dislikes'

I can get the sum from one table but cannot get it to work correctly with multiple tables,

The table structures are different for both tables, I cannot find any questions that are the same as mine..

Here is my 1st table "articles"

username likes dislikes
Bob 3 -1

Here is my 2nd table "awards"

username amount
Bob 3

In this case it should output 5 but I can't get it to work

 SELECT SUM(rep) AS totalRep FROM 
  (
    SELECT `likes` FROM `articles` UNION ALL
    SELECT `dislikes` FROM `articles` UNION ALL
    SELECT `amount` FROM `awards` UNION ALL
  ) GROUP BY `username`

I have also tried like this..

SELECT SUM(rep) AS totalRep FROM 
  (
    SELECT SUM(likes) FROM `articles` UNION ALL
    SELECT SUM(dislikes) FROM `articles` UNION ALL
    SELECT SUM(amount) FROM `awards` UNION ALL
  ) GROUP BY `username`

and like this..

SELECT rep SUM(totrep) AS totrep FROM 
(
  SELECT likes,dislikes FROM `articles`
  UNION ALL
  SELECT amount FROM `awards`
) GROUP BY `username`

and..

SELECT SUM(likes,dislikes,amount) AS totrep
  FROM (
        SELECT likes,dislikes FROM `articles`
        UNION ALL
        SELECT amount FROM `awards`
       )  GROUP BY `username`

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

孤城病女 2025-02-06 19:52:26

对于文章表,您需要总结喜欢+不喜欢的内容,然后使用所有联合。

尝试:

select username,sum(amount) as totalRep
FROM ( 
       select username,
              (sum(likes)+sum(dislikes)) as amount
       from articles
       group by username
    union all
       select username,
              amount
     from awards
     ) as t1
group by  username;     

For the articles table you need to sum likes+dislikes and then use union all.

Try:

select username,sum(amount) as totalRep
FROM ( 
       select username,
              (sum(likes)+sum(dislikes)) as amount
       from articles
       group by username
    union all
       select username,
              amount
     from awards
     ) as t1
group by  username;     

https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=bee2cf836d5a7b76cea65e3b773e880c

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文