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 "<tr>";
echo "<td>{$row['title1']}</td>";
echo "<td>{$row['type']}</td>";
echo "</tr>";
echo "<tr>";
echo "<td>{$row['title2']}</td>";
echo "<td>{$row['type']}</td>";
echo "</tr>";
}
echo "</table>";
select column1 || ' ' || column2 as whole_name FROM tablename; Here || is the concat operator used for concatenating them to single column and ( '' ) inside || used for space between two columns.
The following example shows how to concatenate three different columns: (SELECT id, email1 AS email FROM customer) UNION (SELECT id, email2 AS email FROM customer) UNION (SELECT id, email3 AS email FROM customer) ORDER BY id, email; As you can see, it's important that all the queries return the same columns.
You want to use the function CONCAT If both columns can contain NULL, but you still want to merge them to a single string, the easiest solution is to use CONCAT_WS (): SELECT FirstName AS First_Name , LastName AS Last_Name , CONCAT_WS ('', ContactPhoneAreaCode1, ContactPhoneNumber1) AS Contact_Phone FROM TABLE1
In this tutorial, I show how you can concatenate multiple columns in MySQL. You can simply do this programmatically by separately select fields from MySQL Table and store their values in the single variable after concat their values.
You have two columns – firstname, lastname within your DataBase Table you want to show both the columns values in a single string form. In this case, you can use MySQL functions to combine the values of the columns. Both functions work similarly but have little difference. 1. CONCAT
After you concatenate multiple rows into one column, you can use a reporting tool to plot the result in a table and share them with your team. Here’s an example of a table created using Ubiq. By the way, if you want to create charts, dashboards & reports from MySQL database, you can try Ubiq.
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
+-------+------+
| TITLE | TYPE |
+-------+------+
| asd1 | 7 |
| asd2 | 7 |
| qwe1 | 3 |
| qwe2 | 3 |
+-------+------+
try:
SELECT 'title1' AS 'title', 'type' from 'table'
UNION
SELECT 'title2' AS 'title', 'type' from 'table'
ORDER BY 'title'
Hopefully I got the syntax right.
Essentially you want to do two queries and merge the result, which is what UNION does.
EDIT (see other answer for difference between UNION and UNION ALL).
Use As to rename columns to gt the same structure before using Union
SELECT title1 as title, type
FROM YourTable
UNION ALL
SELECT title2 as title, type
FROM YourTable
You need union
i.e.
select * from (select title1 as title, type
from table
union
select title2 as title, type
from table) order by title
I think you need to use UNION:
SELECT title1, type
FROM YourTable
UNION ALL
SELECT title2, type
FROM YourTable
If you don't want to have duplicates, you can remove use UNION instead of UNION ALL.
--EDIT
As has been suggested, if your intentions were to ORDER these results as well, just add an ORDER BY clause and alias your column:
SELECT title1 title, type
FROM YourTable
UNION ALL
SELECT title2 title, type
FROM YourTable
ORDER BY title
Good luck.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With