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 :
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>";
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.
Thanks for answer, i got
Notice: Undefined index: header
inecho $result['header'].": ".$result['count']."<br>";
– vickey colors
Sep 11 '18 at 7:21