如何从数据库中以以下格式显示数据?

2vuwiymt  于 2021-06-24  发布在  Mysql
关注(0)|答案(2)|浏览(271)

数据库表结构

数据需要显示

Please note: 这个 city_from 以及 city_to 不固定。它来自 city table。但是 single_or_group_id 是固定的 1 = Single 以及 2 = Group .

inb24sb2

inb24sb21#

你应该试试这个。你觉得它会根据你的情况起作用吗

select a.Trans_rent as Single, b.Trans_rent as Group 
from table as a 
inner join table as b on a.city_from = b.city_from and a.city_to = b.city_to 
and b.single_or_group_id = 2 
where a.single_or_group_id = 1
xoshrz7s

xoshrz7s2#

你可以看到这个。希望对你有帮助。

<?php

$map_city = ['2'=>'Madina','3'=>'Makkah'];
$single_group = ['1'=>'Single','2'=>'Group'];

$data = [
    [
        'city_form' => 2,
        'city_to' => 3,
        'single_or_group_id' => 1,
        'rent' => 1000
    ],

    [
        'city_form' => 2,
        'city_to' => 3,
        'single_or_group_id' => 2,
        'rent' => 4000
    ],

];

?>

<table style="width:25%" border=1>

    <?php $is_first = true; ?>
    <?php foreach($data as $single){ ?>

    <?php if($is_first) { ?>
      <tr>
          <th colspan=2><?php echo $map_city[$single['city_form']] .'-'. $map_city[$single['city_to']];?></th>
      </tr>

      <tr>
         <?php foreach($data as $single){ ?>
             <td><?php echo $single_group[$single['single_or_group_id']];?></td>
         <?php } ?>
      </tr>

      <tr>
         <?php foreach($data as $single){ ?>
             <td><?php echo 'S.R '.$single['rent'];?></td>
         <?php } ?>
      </tr>

        <?php $is_first = false; } ?>

    <?php } ?>

</table>

相关问题