如何连接具有相同列名

时间:2018-01-25 07:40:20

标签: php mysql sql

我的表中有一个名称为日期和点的列,以及一个名称日期和列日期的表我想加入它们并在HTML表格中显示它们的数据。 到现在为止我已经完成了: -

    $sql = "SELECT date.date,arabcountry.point,delhidarbar.point,delhiking.point,delhinight.point,disawar.point,faridabad.point,gaziabad.point,lucky7.point,mayapuri.point,peshawar.point,taj.point
FROM date JOIN arabcountry ON date.date = arabcountry.date JOIN delhidarbar ON date.date=delhidarbar.date JOIN delhiking ON date.date=delhiking.date JOIN delhinight ON date.date=delhinight.date JOIN disawar ON date.date=disawar.date JOIN faridabad ON date.date=faridabad.date JOIN gaziabad ON date.date=gaziabad.date JOIN lucky7 ON date.date=lucky7.date JOIN mayapuri ON date.date=mayapuri.date JOIN peshawar ON date.date=peshawar.date JOIN taj ON date.date=taj.date
ORDER BY date.date DESC LIMIT 30";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){ 
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['date'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'] .'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['point'].'</strong></td>';
    echo '</tr>';
    };

但是我在PHP数组中得到的是两列名称日期和点,点只是第一个表格其他表格点不在那里。

2 个答案:

答案 0 :(得分:0)

在查询中使用别名as。例如

SELECT delhidarbar.point as points1, delhiking.point as points2, delhinight.point  as points3 ...

答案 1 :(得分:0)

您可以使用每个字段的别名,如下所示

$sql = "SELECT date.date,arabcountry.point as 'a_point',delhidarbar.point as 'del_point',delhiking.point as 'delhike_point',delhinight.point  as 'delhin_point',disawar.point as 'disawar_point'.....";
$result = mysqli_query($conn, $sql);
while($row = mysqli_fetch_assoc($result)){ 
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['date'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['a_point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['del_point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['delhike_point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['delhin_point'].'</strong></td>';
    echo '<td style="background:#ffffff;"><strong class="fon">'.$row['disawar_point'].'</strong></td>....';

    };