Using mysql aliases to select columns from 2 tables
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
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