我的 LEFT JOIN 和 SUM 之一的结果有问题

发布于 2024-11-07 01:19:57 字数 3159 浏览 2 评论 0原文

所以我有一个关于LEFT JOIN的问题,这段代码根据用户是否获得了该组,为totalPoints返回不同的值。 (如果用户没有获得组或事件,它会返回正确的值)

我只想掌握如何获得LEFT JOIN flow_has_vote ON flow_has _vote.flow_id=flows.id 每次都有效。我之前用三个查询做了一个解决方案,一个获取组和事件规则,一个检查用户是否在考虑安全性的情况下获取了组或事件,一个获取流量...

我想我可以解决这是通过两个查询来实现的,一个查询获取组和事件规则,并检查用户是否获取了组和事件,然后一个查询根据用户应该有权访问它来获取流量。

现在我'我获取一个查询中所需的所有信息,然后使用 IF 语句检查是否应该打印它...

所以,我的问题是,是否有可能获得 SUM(flow_has _vote.points) AStotalPoints 可以这样工作吗?你知道怎么做吗?

而且我也有点好奇,一个查询是处理这个问题的最佳方法吗?当考虑到性能时,使用两个是否合理?

 SELECT 
       flows.id AS flowId,
       flows.security,
       SUM(flow__has__vote.points) AS totalPoints,
       users.id AS userId,
       users.alias,
       flows.event_id AS eventId,
       events.group_id AS groupId,
       events.membershipRules AS eMR,
       groups.membershipRules AS gMR,
       user__has__group.permission AS userHasGroup,
       user__has__event.permission AS userHasEvent
    FROM   
       users,
       events   LEFT JOIN user__has__event ON user__has__event.user_id = '.$userId.',
       groups   LEFT JOIN user__has__group ON user__has__group.user_id = '.$userId.',
       flows    LEFT JOIN flow__has__vote ON flow__has__vote.flow_id=flows.id
    WHERE   
       flows.user_id = users.id AND 
       events.id = flows.event_id AND 
       groups.id = events.group_id AND 
       flows.id='.$flowId

如果您想知道 SQL 语句正在做什么,获取流的信息(帖子)、流所在的事件和组的信息、检查用户对组和事件的访问权限以及获取所有投票对于流程...

这就是表格的样子...

FLOWS id,security,event_id,user_id
USERS id, alias
EVENTS id, name group_id, membershipRules
GROUPS id, name, membershipRules
USER__HAS__GROUP user_id,group_id,permission
USER__HAS__EVENT user_id,event_id,permission
FLOW__HAS__VOTE flow_id,user_id,points

这是我希望的结果...

+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+
| flowId | security | totalPoints | userId | alias  | eventId | groupId | eMR | gMR | userHasGroup | userHasEvent | 
+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+
|      1 | 2        | 1337        | 5      | Pontus | 15      | 2       | 2   | 2   | 4            | 4            | 
+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+

还有一个例子...

+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+
| flowId | security | totalPoints | userId | alias  | eventId | groupId | eMR | gMR | userHasGroup | userHasEvent | 
+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+
|      1 | 2        | 1337        | 6      | Kezia  | 15      | 2       | 2   | 2   | null         | null         | 
+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+

享受你的生活〜Pontus

So I got a question about LEFT JOIN, this code returns different values for totalPoints depending on if the user got the group or not. (if user don't got group or event it returns the correct value)

I just want to grasp how to get the LEFT JOIN flow_has_vote ON flow_has_vote.flow_id=flows.id to work every time. I did a solution before with three query's, one that gets the group and event rule, one that checks if the user got the group or event considering the security and one to get the flow...

And I guess I could solve this by having two query's, one that gets the group and event rules and also check if the user got the group and event and then one that gets the flow depending on the user should have access to it.

Right now I'm getting every information needed in ONE query and then checking with IF statements if it should be printed or not...

So, my question is, is it possible to get the SUM(flow_has_vote.points) AS totalPoints to work this way? And do you know how?

And also I'm a bit curios, is one query the best way to work with this? Would it be justified to use two when you take into account performance?

 SELECT 
       flows.id AS flowId,
       flows.security,
       SUM(flow__has__vote.points) AS totalPoints,
       users.id AS userId,
       users.alias,
       flows.event_id AS eventId,
       events.group_id AS groupId,
       events.membershipRules AS eMR,
       groups.membershipRules AS gMR,
       user__has__group.permission AS userHasGroup,
       user__has__event.permission AS userHasEvent
    FROM   
       users,
       events   LEFT JOIN user__has__event ON user__has__event.user_id = '.$userId.',
       groups   LEFT JOIN user__has__group ON user__has__group.user_id = '.$userId.',
       flows    LEFT JOIN flow__has__vote ON flow__has__vote.flow_id=flows.id
    WHERE   
       flows.user_id = users.id AND 
       events.id = flows.event_id AND 
       groups.id = events.group_id AND 
       flows.id='.$flowId

And if you wonder what the SQL-statement is doing, getting the information for the flow(post), the information about the event and group that the flow is in, checking the user access to the group and event and also getting all the votes for the flow...

This is how the tables looks like...

FLOWS id,security,event_id,user_id
USERS id, alias
EVENTS id, name group_id, membershipRules
GROUPS id, name, membershipRules
USER__HAS__GROUP user_id,group_id,permission
USER__HAS__EVENT user_id,event_id,permission
FLOW__HAS__VOTE flow_id,user_id,points

This is the result I wish for...

+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+
| flowId | security | totalPoints | userId | alias  | eventId | groupId | eMR | gMR | userHasGroup | userHasEvent | 
+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+
|      1 | 2        | 1337        | 5      | Pontus | 15      | 2       | 2   | 2   | 4            | 4            | 
+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+

and one more example...

+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+
| flowId | security | totalPoints | userId | alias  | eventId | groupId | eMR | gMR | userHasGroup | userHasEvent | 
+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+
|      1 | 2        | 1337        | 6      | Kezia  | 15      | 2       | 2   | 2   | null         | null         | 
+--------+----------+-------------+--------+--------+---------+---------+-----+-----+--------------+--------------+

Enjoy your life ~ Pontus

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

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

发布评论

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

评论(1

浮生面具三千个 2024-11-14 01:19:57

因此,基本上要点(恕我直言)是不要在 WHERE 子句中包含 LEFT JOINed 的表条件,因为这使得 LEFT JOIN > 行为类似于INNER JOIN

首先尝试这个查询(尽管我确信您必须进行调整,因为我不确定您到底想要什么结果,稍后会详细介绍):

SELECT
       flows.id AS flowId,
       flows.security,
       SUM(flow__has__vote.points) AS totalPoints,
       users.id AS userId,
       users.alias,
       flows.event_id AS eventId,
       events.group_id AS groupId,
       events.membershipRules AS eMR,
       groups.membershipRules AS gMR,
       user__has__group.permission AS userHasGroup,
       user__has__event.permission AS userHasEvent
FROM users,
LEFT JOIN user__has__event 
     ON user__has__event.user_id = users.id,
LEFT JOIN events 
     ON user__has__event.event_id = events.id

LEFT JOIN user__has__group 
     ON user__has__group.user_id = users.id,
LEFT JOIN groups 
     ON user__has__group.group_id = groups.id 
     AND groups.id = events.group_id

LEFT JOIN flows 
     ON flows.user_id = users.id 
     AND events.id = flows.event_id 
     AND flows.id='.$flowId'
LEFT JOIN flow__has__vote 
     ON flow__has__vote.flow_id = flows.id

WHERE users.id = '.$userId.'
GROUP BY users.id

在这里,我将所有内容LEFT JOINed用户,并且还按用户分组。我有一种感觉,您可能希望通过 (flows.id?, events.id?) 将列添加到组中

另外,您可能希望将一些 < code>LEFT JOINs 到 JOIN,因此您将仅获得具有“流程”的用户。

So, basically the main point (IMHO) is not to include conditions on tables you LEFT JOINed in the WHERE clause, since this makes the LEFT JOIN behave like an INNER JOIN.

Start with trying this query (although I am sure you will have to make adjustments as I am not sure exactly what you want as a result, more about this later):

SELECT
       flows.id AS flowId,
       flows.security,
       SUM(flow__has__vote.points) AS totalPoints,
       users.id AS userId,
       users.alias,
       flows.event_id AS eventId,
       events.group_id AS groupId,
       events.membershipRules AS eMR,
       groups.membershipRules AS gMR,
       user__has__group.permission AS userHasGroup,
       user__has__event.permission AS userHasEvent
FROM users,
LEFT JOIN user__has__event 
     ON user__has__event.user_id = users.id,
LEFT JOIN events 
     ON user__has__event.event_id = events.id

LEFT JOIN user__has__group 
     ON user__has__group.user_id = users.id,
LEFT JOIN groups 
     ON user__has__group.group_id = groups.id 
     AND groups.id = events.group_id

LEFT JOIN flows 
     ON flows.user_id = users.id 
     AND events.id = flows.event_id 
     AND flows.id='.$flowId'
LEFT JOIN flow__has__vote 
     ON flow__has__vote.flow_id = flows.id

WHERE users.id = '.$userId.'
GROUP BY users.id

Here, I LEFT JOINed everything to the user, and also grouped by the user. I have a feeling you will want to add columns to the group by (flows.id?, events.id?)

Also, you may want to turn some of the LEFT JOINs to JOIN, so you will get only users who have a 'flow', for example.

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