简体   繁体   中英

MYSQL join table on sum in range

I have issue with joining table, these table dose not have relation with other tables.

tables structure

users

╔════╦═════════════╗
║ ID ║ Name        ║
╠════╬═════════════╣
║ 1  ║ Jon         ║
║ 2  ║ Mark        ║
║ 3  ║ Tom         ║
╚════╩═════════════╝

Opportunities

╔════╦════════════╦═════════════╗
║ ID ║ user_id    ║ total_price ║
╠════╬════════════╬═════════════╣
║ 1  ║ 2          ║ 1500        ║
║ 2  ║ 2          ║ 2000        ║
║ 3  ║ 1          ║ 1000        ║
╚════╩════════════╩═════════════╝

levels

╔════╦════════════╦═════════════╦══════════╦════════╗
║ ID ║ Name       ║ parent_id   ║ min      ║target  ║
╠════╬════════════╬═════════════╬══════════╬════════╣
║ 1  ║ Golden     ║      0      ║ 1000     ║  5000  ║
║ 2  ║ golden-1   ║      1      ║ 1000     ║  2500  ║
║ 3  ║ golden-2   ║      1      ║ 2551     ║  5000  ║
║ 4  ║ Silver     ║      0      ║ 500      ║  999   ║
║ 5  ║ Silver-1   ║      4      ║ 500      ║  750   ║
╚════╩════════════╩═════════════╩══════════╩════════╝

I want to get user level if summation of his opportunities total price in range between level min and target I tried with this query but it's showing error [HY000][1111] Invalid use of group function

 SELECT
  `users`.`id`                   AS `user_id`,
  `users`.`name`                 AS `user_name`,
  SUM(opportunities.total_price) AS `total_target`,
`levels`.`name`                  As `Level_name`
FROM `users`
  INNER JOIN `opportunities` ON `users`.`id` = `opportunities`.`user_id`
  INNER JOIN `levels` ON SUM(opportunities.total_price) >= levels.min
                         AND SUM(opportunities.total_price) <= levels.target
WHERE `users`.`deleted_at` IS NULL
GROUP BY user_id

Note:I'm using Laravel 5.2

As other have already pointed out in comments: you cannot use an aggregate function directly in a join. Join operation happens before the aggregation.

You need to do the aggregation before the join - therefore you need to calculate the sums in a subquery. This way you can reference the sums as an ordinary field in the join.

select t.*, levels.name as level_name
from levels l
inner join
(SELECT
  `users`.`id`                   AS `user_id`,
  `users`.`name`                 AS `user_name`,
  SUM(opportunities.total_price) AS `total_target`
 FROM users
 INNER JOIN `opportunities` ON `users`.`id` = `opportunities`.`user_id`
 WHERE `users`.`deleted_at` IS NULL
 GROUP BY users.id, users.name) t on t.total_target>=l.min and t.total_target<=l.target

However, I noticed that you have a hierarchy of levels. The above query will return both the main and the sub level for each user in separate records.

If you really need to do it in a single call the following might get you started in the right direction.

Basically the solution is to select from a derived table and join to that instead.

I haven't gotten to test it, so there may very well be syntax errors.

select
 c.`user_id`    `user_id`,
 c.`user_name`  `user_name`,
 c.total_target `total_target`,
 d.`Name`       `Level_name`
from
(select
 `id`             `user_id`,
 `name`           `user_name`,
 sum(total_price) `total_target`
from `users` a
join `opportunites` b
on a.ID = b.user_id
group by user_id) c
join levels d
on c.total_target >= d.min
 and c.total_target =< d.target

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM