[英]How can I join two tables apropreately in (My)SQL?
I need to join two tables together.我需要将两个表连接在一起。 I know, that there's a JOIN in MySQL, but those results I've got weren't that good for my purpose.我知道,在 MySQL 中有一个 JOIN,但是我得到的结果对我的目的来说并不是那么好。
Example:
Say, I have two tables:
a =
{[1, 2]}
{[3, 4]}
b =
{[5, 6]}
{[7, 8]}
When I now join them using the JOIN-command in MySQL, the result is somewhat like that:当我现在使用 MySQL 中的 JOIN 命令加入它们时,结果有点像这样:
result =
{[1, 2], [5, 6]}
{[3, 4], [5, 6]}
{[1, 2], [7, 8]}
{[1, 2], [7, 8]}
but what I need is something like但我需要的是类似
result =
{[1, 2], [5, 6]}
{[3, 4], [7, 8]}
If anyone has any idea how to do that with a rather simple SQL-script, I would be very thankful:D如果有人知道如何使用相当简单的 SQL 脚本来做到这一点,我将非常感激:D
Greetings:)问候:)
You don't have a join
key.您没有join
密钥。 You seem to want the data "side-by-side".您似乎希望数据“并排”。
You can do this using row_number()
to generate a join
key:您可以使用row_number()
来生成join
键:
select a.*, b.*
from (select a.*,
row_number() over () as seqnum
from a
) a join
(select b.*,
row_number() over () as seqnum
from b
) b
on a.seqnum = b.seqnum;
Note: This is not guaranteed to preserve the ordering of the rows in the tables.注意:这不能保证保留表中行的顺序。 It will just put the values side-by-side.它只会将值并排放置。
In older versions, you can use variables for this purpose:在旧版本中,您可以为此目的使用变量:
select a. select , b. ,乙。 from (select a. , (@rna:= @rna + 1) as seqnum from a cross join (select @rna:= 0) params ) a join (select b. , (@rnb:= @rnb + 1) as seqnum from b cross join (select @rnb:= 0) params ) b on a.seqnum = b.seqnum; from (select a. , (@rna:= @rna + 1) as seqnum from a cross join (select @rna:= 0) params) a join (select b. , (@rnb:= @rnb + 1) as seqnum from b cross join (select @rnb:= 0) params ) b on a.seqnum = b.seqnum;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.