如何将SQL表中的某些条件与表记录结合起来



我在数据库中有六个表,所有表都是相对的,并且希望在一个表中显示记录。

以下是我的表格:

1( mls_stores

*----------------------------*
|   store_id |  store_title  |
*----------------------------*
|     1001   |  ajmar-jaipur |
|     1002   |  dwarka-delhi |
*----------------------------*

2( mls_类别

*-------------------------------------------*
| cat_no | store_id |  cat_value | cat_type |
*-------------------------------------------*
|  20    |   1001   |     1      | running  |
|  21    |   1001   |     4      | cycling  |
|  22    |   1002   |     1      | running  |
|  23    |   1002   |     2      | swmining |
*-------------------------------------------*

3( mls_points_matrix

*----------------------------------------*
| store_id | value_per_point |  maxpoint |
*----------------------------------------*
|   1001   |       1         |    10     |
|   1001   |       2         |    20     |
|   1002   |       1         |    20     |
|   1002   |       4         |    30     |
*----------------------------------------*

4( mls_user

*--------------------------*
| id |  store_id |  name   |
*--------------------------*
|  1 |  1001     | sandeep |
|  2 |  1001     | jagveer |
|  3 |  1002     | gagan   |
*--------------------------*

5( bonus_points

*---------------------------------------------------*
| user_id |  store_id |  bonus_points |  bonus_type |
*---------------------------------------------------*
|    1    |    1001   |     10        |   fixed     |
|    3    |    1002   |      2        |     %       |
*---------------------------------------------------*

6( mls_entry

*-------------------------------------------------------*
| user_id | store_id |  category |  distance |  status  |
*-------------------------------------------------------*
|   1     |   1001   |    20     |    10     | approved |
|   1     |   1001   |    21     |    40     | approved | 
|   1     |   1001   |    20     |     5     | reject   | 
|   2     |   1001   |    21     |    40     | approved | 
|   3     |   1002   |    22     |    10     | approved | 
|   3     |   1002   |    23     |    20     | approved |
*-------------------------------------------------------*

现在我想要输出如下:

*-----------------------------------------------------------------------------------*
| Name    |  Entries   | Points Earned | Bonus Points | Total Points | Total Amount |
*-----------------------------------------------------------------------------------*
| Sandeep | running(1) |       20      |      10      |     30       |     60       |
|         | cycling(1) |               |              |              |              |    
*-----------------------------------------------------------------------------------*
| Jagveer | cycling(1) |       10      |      0       |     10       |     10       |    
*-----------------------------------------------------------------------------------*  

我正在使用以下代码:

SELECT
u.name,
ROUND(COALESCE(t1.points, 0)) AS points,
ROUND(COALESCE(b.bonus_points, 0)) AS bonus_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) AS total_points
FROM mls_user u
LEFT JOIN
(
SELECT e.user_id, e.status, SUM(e.distance / c.cat_value) AS points
FROM mls_entry e
INNER JOIN mls_category c
ON e.store_id = c.store_id AND e.category = c.cat_no
GROUP BY e.user_id
HAVING e.status='approved'
) t1
ON u.id = t1.user_id
LEFT JOIN bonus_points b
ON u.id = b.user_id
WHERE u.store_id = '1001'
ORDER BY
total_points DESC

这个SQL查询给了我赢得的积分、奖励积分和总积分,但我找不到条目和总金额,它给了我错误的Sandeep积分计算,根据数据,一个条目被拒绝。所以应该是20,而不是25。

我的总金额将是Sandeep 30X2(来自点矩阵(=60与jagvier的总量10X1=10相同。

我在DEMO 中创建了表格

请尝试以下操作:

SELECT
u.name,
ROUND(COALESCE(t1.points, 0)) AS points,
ROUND(COALESCE(b.bonus_points, 0)) AS bonus_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) AS total_points,
ROUND(COALESCE(t1.points, 0) + COALESCE(b.bonus_points, 0)) * t1.countId as total_amount,
group_concat(t1.EntriesConcat) as Entries
FROM mls_user u
LEFT JOIN
(
SELECT e.user_id, e.status, SUM(e.distance / c.cat_value) AS points,
concat(c.cat_type, '(',count(e.user_id), ')' ) as EntriesConcat,
count(e.user_id) as countId -- it returns count of records according to group by part
FROM mls_entry e
INNER JOIN mls_category c
ON e.store_id = c.store_id AND e.category = c.cat_no
-- remove HAVING and use WHERE clause
WHERE e.status='approved'
GROUP BY e.user_id
) t1 ON u.id = t1.user_id
LEFT JOIN bonus_points b ON u.id = b.user_id
WHERE u.store_id = '1001'
ORDER BY total_points DESC
mysql的
  • group_concat可用于连接Group By上的值

最新更新