如何比较if
中两个不同表格中的两个单元格?
我们说我有一张 t1 这样的表:
|id |name |number |week |
|1 |Ross |2 |14 |
|2 |Brad |3 |14 |
然后我有一张表 t2 ,如:
|week |rightnum |
|14 |2 |
我想选择两个表,如果数字等于 rightnum 那么它应该echo
绿色的数字。我试过了:
SELECT * FROM t1 JOIN t2 on t1.week = t2.week WHERE t1.week = 14
然后:
while ($row = mysqli_fetch_array($query)) {
if (t1.number == t2.rightnum) {
echo "<font color="green">".$row['k1']."<br />"; }
else { echo $row['k1']; } };
答案 0 :(得分:0)
您可以在应用程序中执行此操作,但如果您真的想在SQL查询中进行值计算,则可以执行此操作。
首先,首先列出您想要的列。然后使用case
获取颜色:
SELECT t1.week,
(CASE WHEN t1.number = t2.rightnum THEN 'green' END) as color
FROM t1 JOIN
t2
ON t1.week = t2.week
WHERE t1.week = 14;
答案 1 :(得分:0)
我用以下方法解决了它:
while ($row = mysqli_fetch_array($query)) {
if ($row['number'] == $row['rightnum'])
$color = 'green';
else {
$color = 'red'; }
然后将其添加到我发布数字的行:
echo '<font color="'.$color.'">'.$row['number'].'</font>';