8

I have 2 tables: table_a and table_b. Both contain a column named 'open'.

table_a
+-------+
| open  |
+-------+
| 36.99 |
| 36.85 |
| 36.40 |
| 36.33 |
| 36.33 |
+-------+

table_b 
+------+
| open |
+------+
| 4.27 |
| 4.46 |
| 4.38 |
| 4.22 |
| 4.18 |
+------+

I'd like to write a query that returns the following

+-------++------+
| open  || open |
+-------++------+
| 36.99 || 4.27 |
| 36.85 || 4.46 |
| 36.40 || 4.38 |
| 36.33 || 4.22 |
| 36.33 || 4.18 |
+-------++------+

I attempt the following query:

select a.open, b.open from  table_a a, table_b b;

This returns a table with every value of table_b.open for each value of table_a.open

+-------++------+
| open  || open |
+-------++------+
| 36.99 || 4.27 |
| 36.99 || 4.46 |
| 36.99 || 4.38 |
| 36.99 || 4.22 |
|   ... || 4.18 |
+   ... ++------+

I can see I'm misunderstanding proper usage of aliases here. Any ideas?

3
  • try this: select a.open as open_a, b.open as open_b from table_a a, table_b b; Commented Mar 3, 2013 at 20:52
  • still getting every value of table_b.open for each value of table_a.open. table_a.open seems to move on to its next value only after being printed next to every table_b.open value Commented Mar 3, 2013 at 20:55
  • Do you have another column you can join them on? Commented Mar 3, 2013 at 20:58

2 Answers 2

7

It is not an alias problem that you have. You are performing a CROSS JOIN on the table which creates a cartesian result set.

This multiplies your result set so every row from table_a is matched directly to every row in table_b.

If you want to JOIN the tables together, then you need some column to join the tables on.

If you have a column to JOIN on, then your query will be:

select a.open as a_open,
  b.open as b_open
from table_a a
inner join table_b b
  on a.yourCol = b.yourCol

If you do not have a column that can be used to join on, then you can create a user-defined variable to do this which will create a row number for each row.

select 
   a.open a_open, 
   b.open b_open
from
(
  select open, a_row
  from
  (
    select open,
      @curRow := @curRow + 1 AS a_row
    from table_a
    cross join (SELECT @curRow := 0) c
  ) a
) a
inner join
(
  select open, b_row
  from 
  (
    select open,
      @curRow := @curRow + 1 AS b_row
    from table_b 
    cross join (SELECT @curRow := 0) c
  ) b
) b
  on a.a_row = b.b_row;

See SQL Fiddle with Demo

Sign up to request clarification or add additional context in comments.

3 Comments

Ah I see. Is there a way around this?
@holiday_cannibalism do you have another column that could be used to join on?
@holiday_cannibalism See my edit, you can use a user defined variable to generate a value to join the tables on.
2

You need a column that may be used to join that two tables.

You can try generating a pseudo-column as a row number, but I'm not sure that it's what you're trying to achieve. This should look like that (can test it right now, but the idea is clear):

SELECT
    a.open, b.open
FROM
    (SELECT
        open, @curRow := @curRow + 1 AS row_number
     FROM
        table_a
     JOIN
        (SELECT @curRow := 0)
    ) a
JOIN
    (SELECT
        open, @curRow := @curRow + 1 AS row_number
     FROM
        table_b
     JOIN
        (SELECT @curRow := 0)
    ) b
ON
    a.row_number = b.row_number

3 Comments

I did indeed have another column(date) and using
So show whole schema for both tables, and say us how you decide which rows should be combined together.
'select a.open as open_a, b.open as open_b from table_a a, table_b b where a.date = b.date' yielded the correct result. Didn't know you could do that with the row number though. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.