不同列表中的订单类型


Order types in different columns table

这是一个

一般性问题,与特定的编程代码没有任何关系。然而,这听起来很简单,但我无法弄清楚。

给出以下数据库结构:

id  |  name   |  type
1   |  testA  |   A
2   |  testA  |   A
3   |  testA  |   A
4   |  testB  |   B
5   |  testB  |   B

由此我想生成如下所示的表:

表格:

Col A   Col B
testA   testB
testA   testB
testA

所以 HTML 代码看起来像:

<table>
   <tr>
     <th>Col A</th>
     <th>Col B</th>
   </tr>
   <tr>
     <td>testA</td>
     <td>testB</td>
   <tr>
   <tr>
     <td>testA</td>
     <td>testB</td>
   <tr>
   <tr>
     <td>testA</td>
     <td></td>
   <tr>
<table>

虽然,如果您有一些使用内联colspan的解决方案,rowspan也欢迎。

我找不到一个优雅的解决方案。

到目前为止,我的代码是:

// Not Working:
$elements = array('doc'=>array(),'pub'=>array());
$db = new SQLite3('sqlite3.db');
$results = $db->query('SELECT * FROM l ORDER BY type;');
while ($row = $results->fetchArray(SQLITE3_ASSOC)) {
    array_push($elements[$row['type']], $row);
}
$fi = array();
for ($i=0;$i<count($elements['pub'])+count($elements['doc']);$i++){
    if ($i%2==0){
        array_push($fi,$elements['pub'][floor($i/2)]);
    }else{
        array_push($fi,$elements['doc'][floor($i/2)]);
    }
}
var_dump($fi);
exit();
// This is not working because most of the times 
// the count of elements from type A
// is NOT equal to elements of type B.

同一行中的 A/B 值之间没有联系,所以我们必须发明一个。这可以通过使用新插入的行的 rowid 来完成(SELECT 只执行完整的外部连接):

CREATE TEMPORARY TABLE tempA AS
SELECT name FROM l WHERE type = 'A';
CREATE TEMPORARY TABLE tempB AS
SELECT name FROM l WHERE type = 'B';
SELECT tempA.name, tempB.name
FROM tempA
LEFT JOIN tempB USING (rowid)
UNION ALL
SELECT tempA.name, tempB.name
FROM tempB
LEFT JOIN tempA USING (rowid)
WHERE tempA.name IS NULL;
DROP TABLE tempA;
DROP TABLE tempB;