我想要unix中的paste命令,它需要两个文件并打印第一行,第一个文件的第一行,然后是分隔符,然后是第二个文件的第一行,然后是换行符,然后是第一个文件的第二行分隔符,第二个文件的第二行,等等。
所以我想在sql中这样做,从两个表中提取列,输出结果,从第一个表和第二个表中提取第一行的列(作为一行),然后从两个表中提取第二行,等等。不需要交叉连接,比如从第一个表中提取第一行,从第二个表中提取第二行等等。这可能吗?很难在网上找到这个。。。
编辑:

Table 1:          Table 2:
column bla        column cla
a                 80
z                 7
f                 15
k

Expected result:
column bla, column cla
a, 80
z, 7
f, 15
k, NULL

很简单:),除了一点也不。。。
编辑2:
请不要使用@variables

最佳答案

create table if not exists first_40482804 (
    bla varchar(50)
) ;

create table if not exists second_40482804 (
    cla int
) ;

truncate table first_40482804 ;
truncate table second_40482804 ;

insert into first_40482804 ( bla ) values ('a') ;
insert into first_40482804 ( bla ) values ('z') ;
insert into first_40482804 ( bla ) values ('f') ;

insert into second_40482804 ( cla ) values ( 80 ) ;
insert into second_40482804 ( cla ) values ( 7 ) ;
insert into second_40482804 ( cla ) values ( 15 ) ;

set @blaRow = 0 ;
set @claRow = 0 ;

select concat( `first`.bla,',',`second`.cla) paste from
(
    select @blaRow:=@blaRow+1 `row` , bla from first_40482804
) `first`
left join
(
    select @claRow:=@claRow+1 `row` , cla from second_40482804
) `second`
on `first`.`row` = `second`.`row`

结果
paste
a,80
z,7
f,15

关于mysql - mysql“粘贴”两个结果在一起(并排),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40482804/

10-12 00:14
查看更多