【问题标题】:Joining 2 SQL tables, split rows into new columns连接 2 个 SQL 表,将行拆分为新列
【发布时间】:2014-12-18 21:16:46
【问题描述】:

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>    

【问题讨论】:

  • 第二张表中的数值代表什么?
  • 例如订单中支付了多少24%的税

标签: php mysql join split multiple-columns


【解决方案1】:

棘手部分的伪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中查询并正确显示。

【讨论】:

  • 我同意你的观点,这是“正确”的做法。但是我认为这不应该在生产系统中使用。关系数据库应使用关系数据库,不应与此类查询合并。
  • 好的,代码在上面添加了,我得到这个错误:致命错误:未捕获异常'ErrorException',消息'错误:子查询返回超过1行
    错误编号:1242
    选择 order_id, firstname, lastname, total, (select value from oc_order_total where title='ALV 24%') as tax24, (select value from oc_order_total where title='ALV 14%') as tax14 , (select value from oc_order_total where title='ALV 9%') as tax9 from oc_order' in /
  • 您应该添加类似于子查询的内容:and table2.id=table1.id(我也更改了反映这一点的答案。)
【解决方案2】:

table2.code 必须包含类似 "24","14","24".. 而不是 "Tax 24%" 的值

【讨论】:

    猜你喜欢
    • 2023-03-19
    • 1970-01-01
    • 1970-01-01
    • 2019-01-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-05
    • 2022-12-22
    相关资源
    最近更新 更多