简体   繁体   English

区分联合中的两个表

[英]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?到目前为止,我正在尝试从这两个表中回显结果,此代码工作正常,但是我需要实现user_statement_lost的所有结果以红色回显,并且所有user_statement_won以绿色回显? 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;

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):您可以为每个查询添加一个标志,表明它属于哪个组,如下所示,我添加了文本来区分它们,但它可以是任何东西(0/1、TRUE/False 等):

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM