MySQL 查询闭包表

发布于 2024-12-03 14:55:49 字数 1993 浏览 0 评论 0原文

当前 USER 表:

Field       Type        
id          int(11)     
firstname   varchar(64)     
lastname    varchar(64)     
status      varchar(5)  
permission  smallint(5) 

当前 USER_RELATIONS 表:(它有 2 个外键 - 祖先 - > USER.id - 后代 - > USER.id)

Field       Type 
relationId  int(11)
ancestor    int(11)
descendant  int(11)
length      int(11)

当前 TRANSACTIONS 表:(它有 1 个外键 - chid - > USER.id)

Field   Type
id      int(11)
chid    int(11)
date    date
amt     varchar(16)

所有关系均已正确设置,并在用户加入并输入引用他的另一个用户的引用时创建,从而创建层次结构树。

之前,我试图正确地进行此设置,并得到了“Puggan Se”的大力帮助,他为我指明了 Closure Tables 的方向。

目前,我可以看到单个原始推荐人(祖先)及其所有被推荐用户(后代)的整个树。我们还设置了一个自动分级系统,可以对推荐了预定义数量后代的每个用户进行分级。

我们现在已经添加了一个商店,希望给每个用户一点东西,供他们参考并邀请其他用户加入和购物。 “小东西”基本上是销售额的百分比,基于其树结构。

解释:

1: A invited B, C & D -> B invited E,F -> C invited G -> D invited no-one
2: A moves to status=2(because he invited 3) -> B moves to status=1 (cause he invited 2)-> C & D remain on status=0 (because minimum required invites = 2)
3: Now when B, C & D purchases something from the shop, A should ge a little something back. Because A is status 2, he will get X % for all status=1 sales and Y % for all status=0 sales
4: In the event that B surpasses A in status, A will NOT get a "little something" back.
5: status=0 levels do not get something back.

问题: 我希望有人检查 MySQL 查询并告诉我是否正确执行。我想获取祖先树中所有后代的总交易和总支出,其中后代状态 <祖宗地位。有人可以帮忙吗?目前,我为每个关系运行 4 次。length/status=,因为有 4 个状态级别低于最高级别 4。

查询:

select COUNT(*) as total, SUM(amt) as amount from transactions
  left join card_holders_relations as t1 on transactions.chid = t1.descendant
  left join card_holders as t2 on t2.id = t1.descendant
  where t1.ancestor = '3'
  AND t2.status = 0
  AND t1.length = 4;

现在每次 t2.status 增加到不等于祖先状态并且 t1.length 减少时到 1,因为 length = 0 是祖先本身。

我的假设和方法正确还是有更简单的方法?

Current USER table:

Field       Type        
id          int(11)     
firstname   varchar(64)     
lastname    varchar(64)     
status      varchar(5)  
permission  smallint(5) 

Current USER_RELATIONS table: (it has 2 FOREIGN KEYs - ancestor->USER.id - descendant->USER.id)

Field       Type 
relationId  int(11)
ancestor    int(11)
descendant  int(11)
length      int(11)

Current TRANSACTIONS table: (it has 1 FOREIGN KEY - chid->USER.id)

Field   Type
id      int(11)
chid    int(11)
date    date
amt     varchar(16)

All the relations are setup correctly and are created when a user joins and enters a reference of another user that has referred him, thus creating a hierarchical tree.

Previously I was trying to get this setup correctly and had some great help from "Puggan Se", whom pointed me in the direction of Closure Tables.

Currently I can see an entire tree for a single original referrer (ancestor) with all its referred users (descendant). We have also setup an auto leveling system that will level each user that have referred a pre-defined number of descendants.

We have now added a shop and would like to give each user a little something back for their references and inviting other users to join and shop. The "little something" is basically a percentage of the sale, based on their tree structure.

Explained:

1: A invited B, C & D -> B invited E,F -> C invited G -> D invited no-one
2: A moves to status=2(because he invited 3) -> B moves to status=1 (cause he invited 2)-> C & D remain on status=0 (because minimum required invites = 2)
3: Now when B, C & D purchases something from the shop, A should ge a little something back. Because A is status 2, he will get X % for all status=1 sales and Y % for all status=0 sales
4: In the event that B surpasses A in status, A will NOT get a "little something" back.
5: status=0 levels do not get something back.

Question:
I want someone to review the MySQL query and tell me if I am doing it correctly. I want to get the total transactions and total amount spend by all descendants in an ancestor's tree where the descendant status < ancestor status. Can someone please help? Currently I run it 4 times for each relations.length/status= since there are 4 status levels below the highest which is 4.

The Query:

select COUNT(*) as total, SUM(amt) as amount from transactions
  left join card_holders_relations as t1 on transactions.chid = t1.descendant
  left join card_holders as t2 on t2.id = t1.descendant
  where t1.ancestor = '3'
  AND t2.status = 0
  AND t1.length = 4;

Now each time the t2.status increases to NOT equal the ancestor status and the t1.length decreases to 1, since length = 0 is the ancestor himself.

Are my assumptions and methods correct or is there an easier way of doing this?

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

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

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文