簡體   English   中英

如何將表記錄與SQL表中的某些條件合並?

[英]How to combine table records with some condition in SQL table?

我在數據庫中有六張表,所有表都是相對的,想在一張表中顯示記錄。

以下是我的表格:

1) mls_stores

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

2) mls_category

*-------------------------------------------*
 | 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 與 jagveer 相同,jagveer 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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM