按列值排序mySQL结果

时间:2017-12-26 06:24:41

标签: php mysql sorting

Example

参考附图中的表格"示例"。

我们的卡丁车赛车俱乐部使用电子商务软件进行在线注册。每个注册在 coCartID 列中都有唯一的编号。然后注册的所有细节(驱动程序名称,卡丁车#等等)具有与选项相同的状态,因此它们都出现在同一列 coCartOption 中,并由<中的标题标识em> coOptID 专栏。

我设法显示最近注册的排序列表:

$sql = "
SELECT coCartID, coOptGroup, coCartOption, coOptID
FROM cartoptions
WHERE coCartID > 2200 AND (coOptGroup = '*Driver Name:' OR coOptGroup = '*1st Class - $85:' OR coOptGroup = '1st Class - $100:' OR coOptGroup = '*1st Class Kart #:' OR coOptGroup = '1st Transponder #:' OR coOptGroup = '2nd Class - $25:' OR coOptGroup = '2nd Class Kart #:' OR coOptGroup = '2nd Transponder #:')
ORDER BY coCartID ASC, coOptID DESC
";
$result = $conn->query($sql);

if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
echo $row["coOptGroup"] . $row["coCartOption"] . "<br>";
    }
} else {
echo "0 results";
}
$conn->close();
?> 

使用上面的代码,信息在每一行之间显示中断,我只能通过将 coOptID 设置为升序或降序来控制订单:

Kari Hillwig
33个
高级世界公式 阿曼康奈尔 9193664个
100cc大师
99个
Fred Blauvelt
高级世界公式 77

问题1:如何将所有条目与相同的coCartID组合并显示在一行中。我用fetch_assoc,fetch_array和&amp ;;花了几个令人沮丧的时间。 fetch_row,但无论我能做什么都不能让中断放在正确的位置:

Kari Hillwig,Sr。World Formula,33
Arman Cornell,100cc Master,99,9193664
Fred Blauvelt,高级世界公式,77岁

问题2:除了ASC或DESC之外,是否可以按特定顺序对 coOptID 数据进行排序? (如果是这样,我可以按相同的顺序显示相应的 coCartOption 数据。)

提前谢谢你,

2 个答案:

答案 0 :(得分:0)

您可以使用以下查询为每个coCartID按行获取固定号码的数据。 coOptGroup(s)

SELECT coCartID,
MAX(CASE WHEN coOptGroup = '*Driver Name:' THEN coCartOption ELSE NULL END) DriverName,
MAX(CASE WHEN coOptGroup IN('*1st Class - $85:','1st Class - $100:' ) THEN coCartOption ELSE NULL END) 1stClass,
MAX(CASE WHEN coOptGroup = '*1st Class Kart #:' THEN coCartOption ELSE NULL END) 1stClassKart,
MAX(CASE WHEN coOptGroup = '1st Transponder #:' THEN coCartOption ELSE NULL END) 1stClassTransponder,
MAX(CASE WHEN coOptGroup ='2nd Class - $25:'  THEN coCartOption ELSE NULL END) 2ndClass,
MAX(CASE WHEN coOptGroup = '2nd Class Kart #:' THEN coCartOption ELSE NULL END) 2ndClassKart,
MAX(CASE WHEN coOptGroup = '2nd Transponder #:' THEN coCartOption ELSE NULL END) 2ndClassTransponder
FROM cartoptions
WHERE coCartID > 2200
AND (coOptGroup = '*Driver Name:' 
    OR coOptGroup = '*1st Class - $85:' 
    OR coOptGroup = '1st Class - $100:' 
    OR coOptGroup = '*1st Class Kart #:' 
    OR coOptGroup = '1st Transponder #:' 
    OR coOptGroup = '2nd Class - $25:' 
    OR coOptGroup = '2nd Class Kart #:' 
    OR coOptGroup = '2nd Transponder #:'
)
GROUP BY coCartID
ORDER BY coCartID ASC

DEMO

当您执行上述查询时,您将coCartID中与$row中的单$row["DriverName"]相关的所有值,例如$row['1stClass']vector.sort(new myComparator()); ...在每次循环迭代中

答案 1 :(得分:0)

我会使用带有GROUP_CONCAT()ORDER BY参数的SEPARATOR以及IN子句中的WHERE来执行此操作。

演示:http://sqlfiddle.com/#!9/141e5f/6

SELECT GROUP_CONCAT(coCartOption
ORDER BY
    FIELD (coOptGroup,
           '*Driver Name:',
           '*1st Class - $85:',
           '1st Class - $100:',
           '*1st Class Kart #:',
           '1st Transponder #:',
           '2nd Class - $25:',
           '2nd Class Kart #:',
           '2nd Transponder #:')
    SEPARATOR ', ') AS csv
FROM cartoptions
WHERE coCartID>2200 AND 
      coCartOption!='(N/A)' AND
      coOptGroup IN (
          '*Driver Name:',
          '*1st Class - $85:',
          '1st Class - $100:',
          '*1st Class Kart #:',
          '1st Transponder #:',
          '2nd Class - $25:',
          '2nd Class Kart #:',
          '2nd Transponder #:')
GROUP BY coCartID
ORDER BY coCartID

此查询应该在将来很容易理解和更新。

请参阅我的演示链接,了解干净的三行单列结果集。