简体   繁体   中英

join table A first row with table B first row without common identifiers (for all rows) . Mysql

I messed up with identifiers and my two tables can't be joined by any column. But their order is the same. First row in table A is first row in table B, second row in table A is second row in table B and the same with rest. Is there any way to join those two tables by their row number or something like that?

For example:

Table A:

two
three
one


Table B:

cat
dog
mouse


Expected result would be:

two - cat
three - dog
one - mouse

Or is it not possible and I need to start everything all over again ?

You could add an auto increment column to each table:

ALTER TABLE TableA ADD COLUMN (id INT AUTO_INCREMENT PRIMARY KEY);
ALTER TABLE TableB ADD COLUMN (id INT AUTO_INCREMENT PRIMARY KEY);

Then you can do a join

SELECT num, animal
FROM TableA
JOIN TableB ON TableA.id = TableB.id

Output:

num     animal  
two     cat
three   dog
one     mouse

Use the query

select a.c1, b.c1
from
(select c1 ,@rownum:=@rownum+1 as rn1 from taba,(SELECT @rownum:=0) r ) a
join
(select c1 ,@rownum1:=@rownum1+1 as rn2 from tabb,(SELECT @rownum1:=0) s ) b
on a.rn1 = b.rn2

SQL Fiddle :- http://sqlfiddle.com/#!9/a046b5/22

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM