我想将共享同一“城市”列的机场分组在一起。 现在它们都并排列出,这很好,但我想为每个拥有两个机场的城市添加一个额外的选项标签。
例如现在我的代码显示:
纽约 - 肯尼迪机场
纽约 - 拉瓜迪亚
洛杉矶 - LAX
但我希望它显示:
纽约 - 全部
纽约 - 肯尼迪机场
纽约 - 拉瓜迪亚
洛杉矶 - LAX
感谢任何帮助。提前致谢
$getairportsfrom=mysqli_query($db,"SELECT * FROM details WHERE type='Airport' ORDER BY city");
while($rowfrom = mysqli_fetch_array($getairportsfrom)) {
echo "<option value='".$rowfrom['title']."'>" . $rowfrom['city'] . " - " . $rowfrom['title'] . "</option>";
}
最佳答案
如果这是您的查询:
SELECT city, title
FROM details
WHERE type = 'Airport'
ORDER BY city;
您可以通过此查询获得您想要的内容:
select city, title
from ((select city, 'All' as title, 1 as ordering
from details
group by city
having count(*) > 1
) union all
(select city, title, 2 as ordering
from details
)
) d
order by city, ordering;
SQL 并不是真正为这种类型的演示操作而设计的。不过,这是可以做到的。
关于php - 将 MySQL 中的查询中的相似条目分组到一起,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25947817/