mysql,选择两个表,然后合并结果
我有两张桌子mysql,选择两个表,然后合并结果,mysql,sql,database,Mysql,Sql,Database,我有两张桌子 table_A A ---- a b c d table_B B ---- 1 2 并且想要像下面那样选择它 A B ---- ---- a 1 b 2 c null d null 我怎样才能做出这样的选择呢。 我需要手术吗?还是只需要“连接语法”? 请帮助我。您可以使用join在sele
table_A
A
----
a
b
c
d
table_B
B
----
1
2
并且想要像下面那样选择它
A B
---- ----
a 1
b 2
c null
d null
我怎样才能做出这样的选择呢。
我需要手术吗?还是只需要“连接语法”?
请帮助我。您可以使用join在select语句中执行此操作 例如:
SELECT a.value, b.value
FROM a
left outer join b on a.id = b.id;
但这要求您拥有主键/外键来关联单独表中的行
您的表需要如下所示:
table_A
--id--value--
123 a
214 b
567 c
455 d
table_B
--id--value--
123 1
214 2
SQL表表示无序的结果。您可以将这些值并排放置(如我将要展示的),但是没有一列指定顺序,每个列表中的顺序是任意的
select max(a) as a, max(b) as b
from ((select (@rna := @rna + 1) as rn, a, null as b
from a cross join (select @rna := 0) params
) union all
(select (@rnb := @rnb + 1) as rn, null as a, b
from b cross join (select @rnb := 0) params
)
) ab
group by rn;
如果您具有和更高的权限,则可以使用窗口功能
select t.a, tb.b
from
(
select a, count(*) over (order by a) rn
from table_a
) t
left join table_b tb on t.rn = tb.b
结果
a b
-------
a 1
b 2
c null
d null
这两个表之间有什么关系?SQL表表示无序集。如果没有列来指定顺序,则无法保证结果。以报纸样式显示不相关的数据通常不使用SQL。改为在两个查询中获取数据,并使用编程语言在网格、HTML表或任何内容中显示所需的数据。