-4

i have mysql query like this

SET @sql = NULL;
SELECT

  GROUP_CONCAT(
      'fvalue AS ',x.name
  ) INTO @sql

from reimbursment x
LEFT JOIN reimbursment_limit y
ON y.acc_code = x.acc_code;

SET @sql = CONCAT('SELECT b.firstname, ', @sql, ' from user b
left join reimbursment_limit c
  on b.id = c.user_id
left join reimbursment d
  on c.acc_code = d.acc_code
group by b.id');

PREPARE stmt FROM @sql;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;

and table like this,

user

id nip    firstname
1  123456 AAAAA
2  999999 BBBBB

reimbursment

id acc_code name
1  22002    Dentis
2  22004    Allowance

reimbursment limit

id user_id acc_code fvalue
1  1       22002    500000
2  1       22004    700000

and result, like this

firstname Dentis Allowance
AAAAA     500000 500000
BBBBB     null   null

The right result, must

firstname Dentis Allowance
AAAAA     500000 700000
BBBBB     null   null

this query is workig, but not give the right result where the allowance for user AAAAA is must 700000, how fix the result?i have try anything but still not give right result.

Thank you

Riski Febriansyah
  • 335
  • 1
  • 8
  • 21

1 Answers1

0

The problem is with the join an group by

SET @sql = CONCAT('SELECT b.firstname, ', @sql, ' from user b
left join reimbursement c -- swapped join clause
  on b.id = c.user_id
left join reimbursment_limit d
  on c.acc_code = d.acc_code'); -- removed group by

SQLFiddle

Tah
  • 1,526
  • 14
  • 22