tags:

views:

49

answers:

3

If i have a table that looks like

num
1
2
3
4
5
6
7
8
9

And i want to display the same table in two columns

SELECT t1.num, t2.num FROM (SELECT * FROM x) AS t1, (SELECT * FROM x) AS t2

So the result set looks like

num
1,1
2,2
3,3
4,4
5,5
6,6
7,7
8,8
9,9

How would i go about doing this in MySQL

EDIT

Sorry i didnt want to make things complex to start with: But here is what im actually trying to do

To clarify a little more, What im actually trying to do is

num
1,2
2,3
3,4
4,5
5,6
6,7
7,8
8,9

what i want to be able to do further:

SELECT t2.num - t1.num FROM ....

Note that the above query will return all 1s but the values in my database are different to what are displayed above

Thanks in Advance

+3  A: 

I don't understand why you don't just do something more simple:

SELECT num AS num1, num AS num2 FROM x

Or if you really want it as a string:

SELECT CONCAT(num, ',', num) AS num FROM x

If you want to select the table twice you could join the table with itself, but I can't see why you would want to do this:

SELECT CONCAT(T1.num, ',', T2.num) AS num
FROM x AS T1
JOIN x AS T2
ON T1.num = T2.num

It would make more sense if you needed fields from different rows, e.g. '1,2', '2,3', etc... Perhaps you have oversimplified things when you made your question? Are you just trying to learn how to use the join syntax?

Mark Byers
I did mean to select 1,2 : 2,3 but i guess i did over simplify the questions... so i have to go with your answer. :D any chance you can do the 1,2 : 2,3
Shahmir Javaid
I think you need to come with some more realistic data. It looks like what you want is to order by something, select the rownum for each row, and join on T1.rownum + 1 = T2.rownum, but that's way more complex than I could write in this tiny comment box, and it's a completely different question from the one you asked - you should probably just make a new question and better describe the problem you want to solve.
Mark Byers
Are you working with time intervals?
Mark Byers
I will post a new question and no im not working with timeintervals
Shahmir Javaid
+2  A: 

You can drop everything else, and just list the column twice in the select statement. So in this case, it'd be:

Select x.num as First_Num, x.num as Second_num from x
aronchick
A: 
SELECT
  CONCAT(num, ',', num) AS num
FROM
  x
Ignacio Vazquez-Abrams