table1的数据库结构如下:
id | name | total
样本数据如下:
1 | Anna | 100,00€
2 | Peter | 1000,00€
3 | John | 10,00€
table2的数据库结构如下:
id | code | value
样本数据如下:
1 | Tax 24% | 20,00€
1 | Tax 14% | 2,00€
2 | Tax 24% | 200,00€
2 | Tax 14% | 20,00€
2 | Tax 24% | 2,40€
here might also be sales with 0% and 9% tax
我希望sql php语句在html表中显示结果:
id | name | Tax with 24% | Tax with 14% | Tax with 9% | total
*
1 | Anna | 20,00€ | 2,00 € | 0% | 100€
我如何用MYSQL PHP HTML写这个?
<?php $query = $this->db->query("select
order_id,
firstname,
lastname,
total,
(select value from `" . DB_PREFIX . "order_total` where title='ALV 24%') as tax24,
(select value from `" . DB_PREFIX . "order_total` where title='ALV 14%') as tax14,
(select value from `" . DB_PREFIX . "order_total` where title='ALV 9%') as tax9
from
`" . DB_PREFIX . "order`");
$products = array();
// Check if there are any rows returned from the query
if ($query->num_rows > 0) {
// Loop through the returned rows for processing
foreach ($query->rows as $result) {
$products[] = array(
'order_id' => $result['order_id'],
'firstname' => $result['firstname'],
'lastname' => $result['lastname'],
'tax24' => $result['tax24'],
'tax14' => $result['tax14'],
'tax9' => $result['tax9'],
'total' => $result['total'],
);
}
}
foreach ($products as $orders) { ?>
<table class="list">
<tr>
<td><?php print $orders['order_id']; ?></td>
<td><?php print $orders['firstname'] . " " . $orders['lastname']; ?></td>
<td><?php print $orders['tax24']; ?></td>
<td><?php print $orders['tax14']; ?></td>
<td><?php print $orders['tax9']; ?></td>
<td><?php print $orders['total']; ?></td>
</tr>
<?php
}
?>
</table>
答案 0 :(得分:0)
table2.code必须包含类似“24”,“14”,“24”的值..而不是“Tax 24%”
答案 1 :(得分:0)
棘手的部分是伪sql:
select
id,
name,
(select value from table2 where code='Tax 24%' and table2.id=table1.id) as tax24,
(select value from table2 where code='Tax 14%' and table2.id=table1.id) as tax14,
(select value from table2 where code='Tax 9%' and table2.id=table1.id) as tax9
from
table1
当然你必须在php中查询并正确显示。