【问题标题】:Want to JOIN fourth table in query想要在查询中加入第四个表
【发布时间】:2018-10-30 11:30:51
【问题描述】:

我有四张桌子:

  1. mls_category
  2. points_matrix
  3. mls_entry
  4. bonus_points

我的第一个表(mls_category)如下:

*--------------------------------*
| cat_no |  store_id | cat_value |
*--------------------------------*
|   10   |    101    |     1     |
|   11   |    101    |     4     |
*--------------------------------*

我的第二张表(points_matrix)如下:

*----------------------------------------------------*
| pm_no |  store_id | value_per_point | maxpoint     |
*----------------------------------------------------*
|   1   |    101    |       1         |      10      |
|   2   |    101    |       2         |      50      |
|   3   |    101    |       3         |      80      |
*----------------------------------------------------*

我的第三张表(mls_entry)如下:

*-------------------------------------------*
| user_id |  category | distance |  status  |
*-------------------------------------------*
|    1    |     10    |    20    | approved |
|    1    |     10    |    30    | approved |
|    1    |     11    |    40    | approved |
*-------------------------------------------*

我的第四张表(bonus_points)如下:

*--------------------------------------------*
| user_id |  store_id | bonus_points | type  |
*--------------------------------------------*
|    1    |    101    |      200     | fixed |
|    2    |    102    |      300     | fixed |
|    1    |    103    |       4      |  per  |
*--------------------------------------------*

现在,我想根据 store_id、user_id 和 type 将奖励积分值添加到总距离的总和中。

我正在使用以下代码来获取总距离:

SELECT MIN(b.value_per_point) * d.total_distance FROM points_matrix b 
JOIN 
(
    SELECT store_id, sum(t1.totald/c.cat_value) as total_distance FROM mls_category c 
    JOIN 
    (
        SELECT SUM(distance) totald, user_id, category FROM mls_entry 
        WHERE user_id= 1 AND status = 'approved' GROUP BY user_id, category
    ) t1 ON c.cat_no = t1.category
) d ON b.store_id = d.store_id AND b.maxpoint >= d.total_distance

上面的代码计算值是正确的,现在我想加入我的第四张表。

这给了我总和 (60*3 = 180) 作为总值。现在,我想要用户 1 的 (60+200)*3 = 780 并存储 id 101 并且值是固定的。

【问题讨论】:

    标签: sql mysqli


    【解决方案1】:

    我认为您的查询将如下所示

    SELECT Max(b.value_per_point)*( max(d.total_distance)+max(bonus_points)) FROM mls_point_matrix b
    JOIN
    (
       SELECT store_id, sum(t1.totald/c.cat_value) as total_distance FROM mls_category c
       JOIN
       (
           SELECT SUM(distance) totald, user_id, category FROM mls_entry
           WHERE user_id= 1 AND status = 'approved' GROUP BY user_id, category
       ) t1 ON c.cat_no = t1.category group by store_id
    ) d ON b.store_id = d.store_id inner join bonus_points bp on bp.store_id=d.store_id
    

    DEMO fiddle

    【讨论】:

    • 感谢您的快速回复,但它正在显示值。我只想将bonus_points 值转换为总计(距离)。
    • @zaynulAbdin 现在出现错误“字段列表中的列 'store_id' 不明确”
    • @SantoshKhatri 已编辑,现在请检查错字
    • @zaynulAbdin 查询如何识别 store_id 的一个问题。意味着我有多个 store_id,我想要 101 个 store_id 数据。
    • 使用 where 条件 where c.store_id=101
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-08
    • 2015-10-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-26
    • 1970-01-01
    相关资源
    最近更新 更多