You are not logged in.

#1 2010-01-10 11:23:29

monotiz
Member
Registered: 2007-10-20
Posts: 140

Mysql ORDER BY question...

Hi, sorry for my bad english.

I have a question for Mysql and ORDER BY function.

I have 2 tables (TabA and TabB for example). In a TabA there is a column DateA with unix time data. In a TabB there is a column DateB with unix time data.

For example:

TabA
+------+---------+
| xxxA | DateA |
+------+---------+
| abc  | 123    |
+------+---------+
| asd  | 126    |
+------+---------+
| rsgf | 127    |
+------+---------+


TabB
+-------+---------+
| xxxB | DateB |
+-------+---------+
| try    | 124    |
+-------+---------+

I would like to merge TabA and TabB and order the row by unix time. For example:

+------+-------+---------+----------+
| xxxA | xxxB | DateA | DateB |
+------+-------+---------+----------+
| abc  |        | 123    |           |
+------+-------+---------+----------+
|        | try   |           | 124     |
+------+-------+---------+----------+
| asd  |        | 126    |           |
+------+-------+---------+----------+
| rsgf |        | 127    |           |
+------+-------+---------+----------+


Thanks.

Offline

#2 2010-01-10 12:24:19

samuele.mattiuzzo
Member
From: Treviso, IT
Registered: 2009-10-12
Posts: 307
Website

Re: Mysql ORDER BY question...

SELECT * FROM tabA, tabB
ORDER BY tabA.dateA, tabB.dateB DESC

with merging i think you want to put all the tabs togheter without a particular condition, so you just simply cross them togheter smile

Offline

#3 2010-01-10 12:27:52

Ramses de Norre
Member
From: Leuven - Belgium
Registered: 2007-03-27
Posts: 1,289

Re: Mysql ORDER BY question...

samuele.mattiuzzo wrote:

SELECT * FROM tabA, tabB
ORDER BY tabA.dateA, tabB.dateB DESC

This will order them by dateA first and then by dateB, I think he wants them sorted on the union of the dates.

Offline

#4 2010-01-10 12:32:40

monotiz
Member
Registered: 2007-10-20
Posts: 140

Re: Mysql ORDER BY question...

Ramses de Norre wrote:

I think he wants them sorted on the union of the dates.

yes....

Offline

#5 2010-01-10 12:33:39

litemotiv
Forum Fellow
Registered: 2008-08-01
Posts: 5,026

Re: Mysql ORDER BY question...

select bla from bla
union
select bla from bla2
order by bla

ᶘ ᵒᴥᵒᶅ

Offline

#6 2010-01-10 12:40:25

monotiz
Member
Registered: 2007-10-20
Posts: 140

Re: Mysql ORDER BY question...

litemotiv wrote:
select bla from bla
union
select bla from bla2
order by bla

But the 2 columns have different name....

Offline

#7 2010-01-10 12:58:07

litemotiv
Forum Fellow
Registered: 2008-08-01
Posts: 5,026

Re: Mysql ORDER BY question...

monotiz wrote:

But the 2 columns have different name....

that's not a problem, each column you define in the select will be added to the result. you can combine columns to a single result but you don't have to:

http://www.mysqltutorial.org/sql-union-mysql.aspx


ᶘ ᵒᴥᵒᶅ

Offline

#8 2010-01-10 13:10:33

monotiz
Member
Registered: 2007-10-20
Posts: 140

Re: Mysql ORDER BY question...

litemotiv wrote:
monotiz wrote:

But the 2 columns have different name....

that's not a problem, each column you define in the select will be added to the result. you can combine columns to a single result but you don't have to:

http://www.mysqltutorial.org/sql-union-mysql.aspx

Ok thanks.. I try it...

Offline

#9 2010-01-10 13:15:49

samuele.mattiuzzo
Member
From: Treviso, IT
Registered: 2009-10-12
Posts: 307
Website

Re: Mysql ORDER BY question...

since columns have different name but the same type, i think you can merge them into one only column and then order that one

Offline

#10 2010-01-10 17:28:36

Mandor
Member
Registered: 2006-06-06
Posts: 154

Re: Mysql ORDER BY question...

That would be a full (outer) join on the two timestamp columns, and after that order by the merged colums. But I'm not really sure about mysql capabilities...


If everything else fails, read the manual.

Offline

Board footer

Powered by FluxBB