使用多行作为一个订单

Order Using Multiple Rows acting as one

我有一个SQL查询

$choice = mysqli_real_escape_string($con,$_POST['choice']);
    
    if($option == "Name")
    $searchresult = "SELECT  id, stationname1, stationinfo1, stationprice1, image1, stationname2, stationinfo2, 
    stationprice2, image2, stationname3, stationinfo3, stationprice3, image3, stationname4, stationinfo4, stationprice4, 
    image4 FROM fuel WHERE stationlocation1 LIKE '%" . $location .  "%' ORDER BY stationname1 $choice"; //search database

它获取数据并按站名 1 排列。问题是我有站名 1 - 4,我想将它们一起排序,以便所有名称都按字母顺序排列,无论列如何。我知道使用

ORDER BY stationname1 ASC, stationname2 ASC, stationname3 ASC, stationname4 ASC

无法使用,因为它是单独订购的。请问我要怎么做才能一起订购?

基本上是

形式的东西
ORDER BY stationname1 and stationname2 and stationname3 and stationname4 ASC

例如...查看此数据库 pic ,我希望它显示:

按照这个顺序

正如其他人所说,您确实应该规范化您的数据,这样您就不会在每个实际行中有效地拥有 4 行数据;但这应该可以满足您的需求(并且可能是规范化数据的途径):

SELECT  id, stationname1 AS stationname, stationinfo1, stationprice1, image1
FROM fuel 
WHERE stationlocation1 LIKE '%" . $location .  "%' 
UNION
SELECT  id, stationname2 AS stationname, stationinfo2, stationprice2, image2
FROM fuel 
WHERE stationlocation2 LIKE '%" . $location .  "%' 
UNION
SELECT  id, stationname3 AS stationname, stationinfo3, stationprice3, image3
FROM fuel 
WHERE stationlocation3 LIKE '%" . $location .  "%' 
UNION
SELECT  id, stationname4 AS stationname, stationinfo4, stationprice4, image4
FROM fuel 
WHERE stationlocation4 LIKE '%" . $location .  "%' 
ORDER BY stationname $choice;

UNION 的最后一个 ORDER BY 子句适用于整个 UNION,除非括号强制它不是这样。