Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to combine two MySQL columns to one column?

Tags:

php

sorting

mysql

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>";
like image 710
user2053384 Avatar asked Feb 08 '13 07:02

user2053384


People also ask

How do I combine two columns into a single column in SQL?

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.

How do I combine two columns of the same table in SQL?

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.

How to merge two columns into one string in MySQL?

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

How to concatenate multiple columns in MySQL?

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.

How to show both columns in a string form in MySQL?

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

How to plot results from multiple rows in a mySQL table?

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.


5 Answers

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 |
+-------+------+

Fiddle: http://sqlfiddle.com/#!2/ff9cf/1

like image 80
Praveen Kumar Purushothaman Avatar answered Oct 22 '22 14:10

Praveen Kumar Purushothaman


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).

like image 24
Joshua Clayton Avatar answered Oct 22 '22 13:10

Joshua Clayton


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
like image 43
Techmonk Avatar answered Oct 22 '22 12:10

Techmonk


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
like image 2
Ed Heal Avatar answered Oct 22 '22 14:10

Ed Heal


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.

like image 2
sgeddes Avatar answered Oct 22 '22 14:10

sgeddes