Display Different texts when counting for mysql union query

Display Different texts when counting for mysql union query



I am using below code to get Count of 2 columns - Reattemptdate & Holddate


<?php
$sql = "SELECT employeename, DATE(reattemptdate) as date,
COUNT(*) as count, 0 as Held FROM orders
WHERE DATE(reattemptdate) = CURDATE()
GROUP BY employeename, date

UNION

SELECT employeename, DATE(holddate) as date,
COUNT(*) as count, 1 as Held FROM orders
WHERE DATE(holddate) = CURDATE()
GROUP BY employeename, date
";
$results = $db_handle->runSelectQuery($sql);
$numrowsresult =$results[0]['count'];


foreach ($results as $result)

echo "Reattempt : ".$result['count']."<br>";



I got result as below , In this first 6 values are from reattemptdate column & next 6 are from holddate column , how to display text Hold instead Reattempt in last 6 lines in below image :



enter image description here




1 Answer
1



You could use logic based on your "Held" column:
Eg:


...
foreach ($results as $result)

if ($result['Held']==0)
$header = "Reattemptdate";
else
$header = "holddate";

echo $header.": ".$result['count']."<br>";



Or add an explicit header to your resultset, something like this:


<?php
$sql = "SELECT employeename, DATE(reattemptdate) as date,
COUNT(*) as count, 0 as Held, 'reattemptdate' as header FROM orders
WHERE DATE(reattemptdate) = CURDATE()
GROUP BY employeename, date

UNION

SELECT employeename, DATE(holddate) as date,
COUNT(*) as count, 1 as Held, 'holddate' as header FROM orders
WHERE DATE(holddate) = CURDATE()
GROUP BY employeename, date
";
$results = $db_handle->runSelectQuery($sql);
$numrowsresult =$results[0]['count'];

foreach ($results as $result)

echo $result['header'].": ".$result['count']."<br>";






Thanks for answer, i got Notice: Undefined index: header in echo $result['header'].": ".$result['count']."<br>";

– vickey colors
Sep 11 '18 at 7:21


Notice: Undefined index: header


echo $result['header'].": ".$result['count']."<br>";






Does it help if you change the case of the Header alias in your sql, to be header?

– Sepster
Sep 11 '18 at 7:25



Header


header






Yes, its working now.... as here : prnt.sc/kt04s0 , how to display one reattempt & one hold value in single line , means reattemptdate : 17, holddate :8 in first line, than others in 2nd, 3rd , 4th lines so on..... because basically i should display values according to employeename.....

– vickey colors
Sep 11 '18 at 7:29



one reattempt & one hold value


reattemptdate : 17, holddate :8






i dont want to display reattemptdate & holddate text, i want to display only values..... please help for that....

– vickey colors
Sep 11 '18 at 7:37


reattemptdate


holddate






Hi Vickey, that sounds like you want a JOIN rather than a UNION. UNION combines rows from multiple queries into a set of rows. But you want to combine COLUMNS from multiple queries into a row.

– Sepster
Sep 11 '18 at 7:37



Thanks for contributing an answer to Stack Overflow!



But avoid



To learn more, see our tips on writing great answers.



Required, but never shown



Required, but never shown




By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

𛂒𛀶,𛀽𛀑𛂀𛃧𛂓𛀙𛃆𛃑𛃷𛂟𛁡𛀢𛀟𛁤𛂽𛁕𛁪𛂟𛂯,𛁞𛂧𛀴𛁄𛁠𛁼𛂿𛀤 𛂘,𛁺𛂾𛃭𛃭𛃵𛀺,𛂣𛃍𛂖𛃶 𛀸𛃀𛂖𛁶𛁏𛁚 𛂢𛂞 𛁰𛂆𛀔,𛁸𛀽𛁓𛃋𛂇𛃧𛀧𛃣𛂐𛃇,𛂂𛃻𛃲𛁬𛃞𛀧𛃃𛀅 𛂭𛁠𛁡𛃇𛀷𛃓𛁥,𛁙𛁘𛁞𛃸𛁸𛃣𛁜,𛂛,𛃿,𛁯𛂘𛂌𛃛𛁱𛃌𛂈𛂇 𛁊𛃲,𛀕𛃴𛀜 𛀶𛂆𛀶𛃟𛂉𛀣,𛂐𛁞𛁾 𛁷𛂑𛁳𛂯𛀬𛃅,𛃶𛁼

Edmonton

Crossroads (UK TV series)