I want to convert this MySQL table:
title1 | title2 |type
-------+--------+----
qwe1 | qwe2 | 3
asd1 | asd2 | 7
to this table in PHP
title | type
------+-----
asd1 | 7
asd2 | 7
qwe1 | 3
qwe1 | 3
But I don't know how to order the PHP table by the first column correctly.
(This is the code I am currently using)
$sql = "SELECT * FROM table ORDER BY title1, title2";
$pager = new PS_Pagination( $dbh, $sql, 3, 4, null );//pagination class
$rs = $pager->paginate();
while ($row = $rs->fetch(PDO::FETCH_ASSOC)){
echo "
";echo "
{$row['title1']}";echo "
{$row['type']}";echo "
";echo "
";echo "
{$row['title2']}";echo "
{$row['type']}";echo "
";}
echo "";
解决方案
In SQL itself you can do it using UNION. And use ORDER BY for the whole query!
SELECT `title1` AS `title`, `type`
FROM `table`
UNION
SELECT `title2` AS `title`, `type`
FROM `table`
ORDER BY `title` ASC
Output
+-------+------+
| TITLE | TYPE |
+-------+------+
| asd1 | 7 |
| asd2 | 7 |
| qwe1 | 3 |
| qwe2 | 3 |
+-------+------+