mysql - 如何将两个表中的值包含在同一列中
问题描述
我有以下查询:
select
t1.x,
t2.y
from table1 t1
join table2 t2
on t1.x = t2.x
这产生了以下结果:
x y1
x y2
x y3
但是,我希望它产生以下内容,其中 t1.x 的值也在第二列中:
x x
x y1
x y2
x y3
是否有捷径可寻?我试图在 PostgreSQL 中实现这一点,但我也对 MySQL 中的任何解决方案感兴趣。
提前致谢!
解决方案
你似乎想要:
select t1.x, t1.x as y
from table1 t1
union all
select t2.x, t2.y
from table2 t2;
仅当join
您想要基于x
在两个表中的行来过滤(或相乘)行时才需要。
推荐阅读
- flutter - 如何创建唯一代码以邀请新用户加入移动应用程序中的群组
- assembly - 在 MIPS 架构中如何“立即”工作?
- c# - 在 C# 的 gridview 中检查何时已批准复选框
- r - 由 sjPlot 包中的 plot_model() 函数生成的奇数比率图对于 R 中的绘图窗口来说太大了
- gcc - 将 OpenEXR 与 Bazel 一起使用?
- python - python - 如何从python中的pandas数据框中打印滚动窗口方程过程?
- angular - 使用“dotnet new angular”创建的 Angular 应用程序的调试在 VSCode 中不起作用
- laravel - web Guard 允许登录,但 admin Guard 不允许登录
- stripe-payments - 是否有允许拆分资金并将其发送到多个银行账户的支付网关
- sql - 多行过滤器和计算百分比