从输出中删除重复行 table
Remove duplicate row from output table
我想删除 table 中的重复输出,table 看起来像这样
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
客户和订单来自我数据库中的不同table
这个table的查询来自一个内连接查询join the two
$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>';
}
上面代码的输出就是第一个例子
我如何实现第二个插图,我的 table 关系有问题还是我应该使用正确的查询?
正如 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'];
}
你最好使用 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。
如果您的数据不是动态的,您可以使用以下方法:
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
检查此查询:
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))
我想删除 table 中的重复输出,table 看起来像这样
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
客户和订单来自我数据库中的不同table
这个table的查询来自一个内连接查询join the two
$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>';
}
上面代码的输出就是第一个例子
我如何实现第二个插图,我的 table 关系有问题还是我应该使用正确的查询?
正如 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'];
}
你最好使用 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。
如果您的数据不是动态的,您可以使用以下方法:
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
检查此查询:
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))