Note that there are some explanatory texts on larger screens.

plurals
  1. POMySQL join same table display rows in columns way (complicated)
    primarykey
    data
    text
    <p>I would like to join the same table 4 times to get columns way to display, I am not sure is it possible do in 1 single SQL statements.</p> <p>tbl_username </p> <pre><code>id username 1 Adam 2 Bob 3 Chris </code></pre> <p>tbl_opportunity </p> <p><img src="https://i.stack.imgur.com/0Xqcx.png" alt="tbl_opportunity"></p> <p>I got these two table, I would like the outcome like this</p> <p><img src="https://i.stack.imgur.com/3qNtk.png" alt="enter image description here"></p> <p>I tested with following sql, but the outcome is wrong</p> <pre><code>SELECT users.`username`, COUNT(`proposal`.name) AS `Pro Total Number Quotation`, CONCAT('RM', FORMAT(SUM(`proposal`.`amount`), 2)) AS pro_rm_amount, COUNT(`nego`.name) AS `nego Total Number Quotation`, CONCAT('RM', FORMAT(SUM(`nego`.`amount`), 2)) AS nego_rm_amount, COUNT(`cw`.name) AS `cw Total Number Quotation`, CONCAT('RM', FORMAT(SUM(`cw`.`amount`), 2)) AS cw_rm_amount, COUNT(`cl`.name) AS `cl Total Number Quotation`, CONCAT('RM', FORMAT(SUM(`cl`.`amount`), 2)) AS cl_rm_amount FROM tbl_username users JOIN `tbl_opportunity` AS proposal ON (proposal.`user_id` = users.id AND proposal.`sales_stage` = 'Proposal' AND proposal.`deleted`=0 AND MONTH(`proposal`.date)= '1' AND YEAR(`proposal`.date)= '2012') LEFT JOIN `tbl_opportunity` AS nego ON (proposal.`user_id` = users.id AND proposal.`sales_stage` = 'Nego' AND nego.`deleted`=0 AND MONTH(`nego`.date)= '1' AND YEAR(`nego`.date)= '2012') LEFT JOIN `tbl_opportunity` AS cw ON (cw.`user_id` = users.id AND cw.`stage` = 'Win' AND cw.`deleted`=0 AND MONTH(`cw`.date)= '1' AND YEAR(`cw`.date)= '2012') LEFT JOIN `tbl_opportunity` AS cl ON (cl.`user_id` = users.id AND cl.`stage` = 'Lose' AND cl.`deleted`=0 AND MONTH(`cl`.date)= '1' AND YEAR(`cl`.date)= '2012') GROUP BY users.`username` </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload