简体   繁体   English

来自两个不同表的两列,在MYSQL中没有任何连接

[英]Two columns from two different table without any join in MYSQL

在此处输入图片说明 I want to know if we can display 2 columns from two different table with out creating any join. 我想知道是否可以在不创建任何联接的情况下显示两个不同表中的2列。

Scenario is that I want to display values present in one column from table1 along with values in column two from Table 2. these two tables dont have any common column to have any kind of join. 方案是,我要显示表1的一列中的值以及表2的第二列中的值。这两个表没有任何公共列可以进行任何连接。

Expected Output: 预期产量:

Column1 Column2
A        1
B        4
C        9
D        13
E
F

Ok, I think I can get you started. 好吧,我想我可以帮助您开始。

Create a UNION query where select the column you want and add a constant column that we'll use later. 创建一个UNION查询,在其中选择所需的列,并添加一个常量列,供以后使用。

SELECT column1 as col , 'T1' as tbl FROM table1;
UNION
SELECT other_column as col , 'T2' as tbl FROM table2;

This will give a table with 2 column 这将给出一个带有2列的表格

col | tbl
----------
 1  | T1
 2  | T1
 a  | T2
...

Then you can make a Pivot table query using 'tbl' as the headers. 然后,您可以使用“ tbl”作为标题进行数据透视表查询。 I'm terrible with Pivot tables, but some research might get you there. 我对数据透视表很糟糕,但是一些研究可能会帮助您实现目标。 Or maybe some will edit this answer to add that information. 也许有人会编辑此答案以添加该信息。

You may also be able to do something with subqueries to get the desired result. 您也许还可以对子查询执行某些操作以获得所需的结果。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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