MySql同一张表上的多个连接-获取多个账本/钱包的期末余额

MySql同一张表上的多个连接-获取多个账本/钱包的期末余额,mysql,sql,laravel,performance,join,Mysql,Sql,Laravel,Performance,Join,事务表架构: -------------------------------------- | user_id | currency | amount | date | -------------------------------------- | 1 | eur | 500 | ... | | 1 | gbp | 100 | ... | | 1 | usd | 10 | ... | | 1

事务
表架构:

--------------------------------------
| user_id | currency | amount | date |
--------------------------------------
|       1 |      eur |    500 | ...  |
|       1 |      gbp |    100 | ...  |
|       1 |      usd |     10 | ...  |
|       1 |      usd |     10 | ...  |
|       1 |      gbp |    100 | ...  |
|       1 |      usd |     10 | ...  |
|       1 |      usd |     10 | ...  |
|       2 |      jpy |     99 | ...  |
|       1 |      jpy |     99 | ...  |
--------------------------------------
我想要的是:

-----------------------------------
|         | total balances        |
| user_id | eur | usd | gbp | jpy |
-----------------------------------
|       1 | 500 |  40 | 200 |  99 |
|       2 |   0 |   0 |   0 |  99 |
-----------------------------------
我的查询代码(使用Laravel的雄辩ORM):

结果SQL:

select 
      SUM(t1.amount) AS total_eur,
      SUM(t2.amount) AS total_gbp,
      SUM(t3.amount) AS total_usd,
      SUM(t4.amount) AS total_jpy,
      u.id AS u_id,
      u.username AS u_username
      
   from `users` as `u` 
   left join `transactions` as `t1` on `t1`.`user_id` = `u`.`id`
   left join `transactions` as `t2` on `t2`.`user_id` = `u`.`id`
   left join `transactions` as `t3` on `t3`.`user_id` = `u`.`id` 
   left join `transactions` as `t4` on `t4`.`user_id` = `u`.`id` 
   
   where `t1`.`wallet_type` = 'eur'
     and `t2`.`wallet_type` = 'gbp'
     and `t3`.`wallet_type` = 'usd'
     and `t3`.`wallet_type` = 'jpy'

   group by `u`.`id`
当使用3个或更多联接时(
transactions
table只有~600行),速度非常慢
当我只使用两种货币/只使用两种连接时,速度相当快(不到1秒)

我认为多次加入同一个表是一种糟糕的做法。

有什么方法可以优化查询吗?

您需要

$query = DB::table('users AS u')
    ->join ('transactions AS t1', 't1.user_id', '=', 'u.id')
    ->selectRaw('
        u.id AS u_id,
        u.username AS u_username,
        SUM(CASE WHEN t1.currency = \'eur\' THEN t1.amount END) AS total_eur,
        SUM(CASE WHEN t1.currency = \'usd\' THEN t1.amount END) AS total_usd,
        SUM(CASE WHEN t1.currency = \'gbp\' THEN t1.amount END) AS total_gbp,
        SUM(CASE WHEN t1.currency = \'jpy\' THEN t1.amount END) AS total_jpy
    ')
    ->groupBy('u.id');
检查语法-我不使用Laravel


如果某些货币未列在某些用户数据中,您将得到NULL。如果需要零,则将表达式扩展到t1.currency=\'xxx\'时的
情况,然后t1.amount ELSE 0结束

$query = DB::table('users AS u')
    ->join ('transactions AS t1', 't1.user_id', '=', 'u.id')
    ->selectRaw('
        u.id AS u_id,
        u.username AS u_username,
        SUM(CASE WHEN t1.currency = \'eur\' THEN t1.amount END) AS total_eur,
        SUM(CASE WHEN t1.currency = \'usd\' THEN t1.amount END) AS total_usd,
        SUM(CASE WHEN t1.currency = \'gbp\' THEN t1.amount END) AS total_gbp,
        SUM(CASE WHEN t1.currency = \'jpy\' THEN t1.amount END) AS total_jpy
    ')
    ->groupBy('u.id');
检查语法-我不使用Laravel


如果某些货币未列在某些用户数据中,您将得到NULL。如果您需要零,那么将表达式扩展到
CASE,当t1.currency=\'xxx\'时,则t1.amount ELSE 0结束

啊,所以我只需要
加入事务
一次?啊,所以我只需要
加入事务
一次?刚刚更新了我的代码,非常有魅力,谢谢!刚刚更新了我的代码,很有魅力,非常感谢!