我想删除表格中的重复输出,表格如下所示
Order No Customer Name Order
1001 John Milk
1001 John Egg
1002 Mark Milk
1002 Mark Bread
我希望它像这样
Order No Customer Name Order
1001 John Milk
Egg
1002 Mark Milk
Bread
订单号和客户名称使用rowspan = 2
客户和订单来自我的数据库中的不同表
此表的查询来自内部联接查询加入两个
$query = mysql_query("select customer.customerName, order.orderNo, order.item
from customer inner join orderInfo on orderInfo.customerId = customer.customerId");
while($order = mysql_fetch_assoc($query))
{
echo '<tr>
<td>'.$order['orderNo'].'</td>
<td>'.$order['customerName'].'</td>
<td>'.$order['item'].'</td>
</tr>';
}
以上代码的输出是第一个插图
我如何实现第二个插图,我的表格关系有问题或我应该使用正确的查询?
答案 0 :(得分:1)
正如jarl所说,这不是一个dbms的事情,按订单号顺序编辑查询并使用下面的代码作为你的第二个插图
$previous=-1000;
while ($row = mysql_fetch_assoc($query)
{
if($previous!=$row['orderNo'])
{
echo "<tr><td>".$row['orderNo']."</td>";
echo "<td>".$row['customerName']."</td>";
echo "<td>".$row['item']."</td>";
}
else
{
echo "<tr><td></td><td></td><td>".$row['item']."</td>";
}
$previous=$row['orderNo'];
}
答案 1 :(得分:0)
您最好使用group_concat:
$query = mysql_query("
SELECT
customer.customerId, customer.customerName, order.orderNo, group_concat(order.item SEPARATOR ',') as order_items
FROM
customer
INNER JOIN
orderInfo on orderInfo.customerId = customer.customerId
GROUP BY customer.customerId
");
在您的代码中,只需替换分隔符:
while($order = mysql_fetch_assoc($query))
{
echo '<tr>
<td>'.$order['orderNo'].'</td>
<td>'.$order['customerName'].'</td>
<td>'.str_replace(',','<br/>',$order['item']).'</td>
</tr>';
}
在这种情况下,你不会需要rowspan = 2.
答案 2 :(得分:0)
如果您的数据不是动态的,您可以使用以下内容:
DECLARE @varTable TABLE (OrderNo NVARCHAR(10), CustomerName NVARCHAR(60), Order NVARCHAR(60))
INSERT INTO @varTable VALUES( CAST(1001 AS NVARCHAR(10)), 'John', 'Milk')
INSERT INTO @varTable VALUES( '', '', 'Egg')
INSERT INTO @varTable VALUES( CAST(1002 AS NVARCHAR(10)), 'Mark', 'Milk')
INSERT INTO @varTable VALUES( '', '', 'Bread')
SELECT * FROM @varTable
答案 3 :(得分:0)
检查此查询:
Select Dep, Case
When rn = 1 And c_d > 1 Then
d
When c_d = 1 Then
d
Else
Null
End D1,
Case
When rn = 1 And c_k > 1 Then
k
When c_k = 1 Then
k
Else
Null
End K1,
Case
When rn = 1 And c_m > 1 Then
m
When c_m = 1 Then
m
Else
Null
End M1
From (Select Count(1) Over(Partition By d,dep Order By d) c_d,
Count(1) Over(Partition By k,dep Order By k) c_k,
Count(1) Over(Partition By m,dep Order By m) c_m,
row_number() Over(Partition By dep Order By dep)rn,
d,
k,
m,
Dep
From (Select 'ali' d, '1000' k, 'M' m, 'x' Dep
From Dual
Union
Select 'ali' d, '1000' k, 'H' m, 'x' Dep
From Dual
Union
Select 'ali' d, '1001' k, 'M' m, 'y' Dep
From Dual
Union
Select 'ali' d, '1000' k, 'H' m, 'y' Dep
From Dual))