如何使具有重复值的列紧贴在一起,而其他值在它们之间留出空格

Posted

技术标签:

【中文标题】如何使具有重复值的列紧贴在一起,而其他值在它们之间留出空格【英文标题】:How do I make the column with duplicate value stick close together while other value leave a space between them 【发布时间】:2021-12-29 00:12:26 【问题描述】:

有谁知道如何使具有相同值的值不会在它们之间留下空白,而具有不同值的值会留下空白?谢谢

Example

ITEMNO DESC STOCKUNIT

[321B3COT4CP08002500000] [DESC Value] [STOCKUNIT Value]

上方和下方彼此靠近。

ITEMNO DESC STOCKUNIT (Same ITEMNO as above but other column value is different)

[321B3COT4CP08002500000] [DESC Value] [STOCKUNIT Value]

上方和下方相互留有空间。

ITEMNO DESC STOCKUNIT (Different ITEMNO Value from above)

[321B3COT4CP08002500000] [DESC Value] [STOCKUNIT Value]

代码:

$table = "SELECT * FROM [TestDB].[dbo].[ICILOC] INNER JOIN [TestDB].[dbo].[ICITEM] ON [TestDB].[dbo].[ICILOC].[ITEMNO]=[TestDB].[dbo].[ICITEM].[ITEMNO] WHERE [QTYONHAND] <> '0' ORDER BY [ICILOC].[ITEMNO] DESC ";
$RS = odbc_exec($connection, $table);

while (odbc_fetch_row($RS)) 
  $ITEMNO = odbc_result($RS, "ITEMNO");
  $DESC = odbc_result($RS, "DESC");
  $STOCKUNIT = odbc_result($RS, "STOCKUNIT");
  $UNITWGT = odbc_result($RS, "UNITWGT");
  $LOCATION = odbc_result($RS, "LOCATION");
  $QTYONHAND = odbc_result($RS, "QTYONHAND");
  $QTYONORDER = odbc_result($RS, "QTYONORDER");
  $QTYSALORDR = odbc_result($RS, "QTYSALORDR");

  if ($ITEMNO = $ITEMNO) 
    echo "<br>";
   

echo "<center><table>";
echo "<th><center>ITEMNO</center></th>";
echo "<th><center>DESC</center></th>";
echo "<th><center>STOCKUNIT</center></th></tr>";
echo "<tr><td>$ITEMNO</td>";
echo "<td>$DESC</td>";
echo "<td>$STOCKUNIT</td></table>";

echo "<center><table><tr>";
echo "<center><th><center>LOCATION</center></th>";
echo "<th><center>QTYONHAND</center></th>";
echo "<th><center>QTYONORDER</center></th>";
echo "<th><center>QTYSALORDR</center></th>";

  echo "<tr><td>$LOCATION</td>";
  echo "<td>$QTYONHAND</td>";
  echo "<td>$QTYONORDER</td>";
  echo "<td>$QTYSALORDR</td></tr>";


echo "</table>";
odbc_close($connection);

【问题讨论】:

【参考方案1】:

你可以做一个if语句,如果不一样就插入一个break

   $table = "SELECT * FROM [TestDB].[dbo].[ICILOC] INNER JOIN [TestDB].[dbo].[ICITEM] ON [TestDB].[dbo].[ICILOC].[ITEMNO]=[TestDB].[dbo].[ICITEM].[ITEMNO] WHERE [QTYONHAND] <> '0' ORDER BY [ICILOC].[ITEMNO] DESC ";
$RS = odbc_exec($connection, $table);

 while (odbc_fetch_row($RS)) 

if ($ITEMNO != odbc_result($RS, "ITEMNO")) 
        echo "<br><br>";
       
      $ITEMNO = odbc_result($RS, "ITEMNO");
      $DESC = odbc_result($RS, "DESC");
      $STOCKUNIT = odbc_result($RS, "STOCKUNIT");
      $UNITWGT = odbc_result($RS, "UNITWGT");
      $LOCATION = odbc_result($RS, "LOCATION");
      $QTYONHAND = odbc_result($RS, "QTYONHAND");
      $QTYONORDER = odbc_result($RS, "QTYONORDER");
      $QTYSALORDR = odbc_result($RS, "QTYSALORDR");
      
    echo "<center><table>";
    echo "<th><center>ITEMNO</center></th>";
    echo "<th><center>DESC</center></th>";
    echo "<th><center>STOCKUNIT</center></th></tr>";
    echo "<tr><td>$ITEMNO</td>";
    echo "<td>$DESC</td>";
    echo "<td>$STOCKUNIT</td></table>";
    
    echo "<center><table><tr>";
    echo "<center><th><center>LOCATION</center></th>";
    echo "<th><center>QTYONHAND</center></th>";
    echo "<th><center>QTYONORDER</center></th>";
    echo "<th><center>QTYSALORDR</center></th>";
    
      echo "<tr><td>$LOCATION</td>";
      echo "<td>$QTYONHAND</td>";
      echo "<td>$QTYONORDER</td>";
      echo "<td>$QTYSALORDR</td></tr>";
    
    
    
    echo "</table>";
    odbc_close($connection);

【讨论】:

现在试试我更新我的答案。我无法测试它,因为无权访问您的数据库。所以请试试这个应该可以的

以上是关于如何使具有重复值的列紧贴在一起,而其他值在它们之间留出空格的主要内容,如果未能解决你的问题,请参考以下文章

如果相同的列值在多行中重复,则 UPDATE - SQL / MYSQL

熊猫数据框:在固定其他列的列中提取具有特定标准/条件最小值的数据[重复]

具有重复值的列上的数据库索引

如何在 Spark/Scala 中查找具有许多空值的列

如何替换列值的前2位,其中值在2019到2099之间

如何将具有嵌套元素的列从其他列添加到数据框(withColumn)[重复]