user14335933
user14335933

Reputation:

distinguish between two tables in a union

I am trying to echo the results from both of these tables so far this code works fine however I need to achieve all the results from user_statement_lost to echo out in red and all of user_statement_won to echo out in green? Is there a way to distinguish between the both. Thanks everyone.

$stmt = $dbh->query('SELECT * FROM `user_statements_lost` WHERE `note` = "CoinFlip" UNION ALL SELECT * FROM `user_statements_won` WHERE `note` = "CoinFlip" ORDER BY `transactionIdentifier` DESC');
$stmt->execute();

$text = "";
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
    echo $row['transactionIdentifier'];
    echo ' - ';
    echo $row['timestamp'];
    echo '<br>';
}
return $text;

Upvotes: 0

Views: 103

Answers (1)

eshirvana
eshirvana

Reputation: 24593

you can add a flag to each query that indicates it is belong to which group as shown below, I added text to distinguish them but it can be anything ( 0/1 , TRUE/False , etc):

SELECT *, "lost" as flag 
FROM `user_statements_lost` 
WHERE `note` = "CoinFlip" 
UNION ALL 
SELECT *, "won" as flag 
FROM `user_statements_won` 
WHERE `note` = "CoinFlip" 
ORDER BY `transactionIdentifier` DESC

Upvotes: 1

Related Questions