SQL加入两个没有关系的表 [英] SQL Join two tables without Relations

查看:167
本文介绍了SQL加入两个没有关系的表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有不同的表具有相同的结构,我想通过他们的列之一加入他们。



问题是他们不会在该列中共享信息。

 表1  - 类型A:

名称|值

表2 - 类型B:

名称|值

结果表:



a single column)

  nameFromA 

nameFromB
...

到目前为止,我尝试过:

 code> SELECT TABLE1.NAME,TABLE2.NAME 
FROM TABLE1,TABLE2
WHERE TABLE1.NAME ='SearchQuery'
OR TABLE2.NAME ='SearchQuery'LIMIT 2;

我知道我的查询是错误的,因为我调用更多的列,我不知道如何加入一列中的一切。


$ p> SELECT TABLE1.NAME
FROM TABLE1
WHERE TABLE1.NAME ='SearchQuery'

UNION

SELECT TABLE2 .NAME
FROM TABLE2
WHERE TABLE2.NAME ='SearchQuery';

您可能想使用 UNION ALL if



要限制结果集,您可以执行以下操作:

  SELECT * FROM(这里是上面的查询)LIMIT 2; 


I have different tables with the same structure, and I would like to join them by one of their columns.

The problem is that they don't share information in that column.

Table 1 - Type A:

Name | Value

Table 2 - Type B:

Name | Value

Resulting table:

(In a single column)

nameFromA

nameFromB
...

So far, I have tried:

SELECT TABLE1.NAME, TABLE2.NAME 
FROM TABLE1, TABLE2 
WHERE TABLE1.NAME = 'SearchQuery'
OR TABLE2.NAME = 'SearchQuery' LIMIT 2;

I know that my query is wrong because I'm calling more columns that what I want, but I am not sure how to join everything in a single column. How can I accomplish this?

解决方案

Have you tried?

SELECT TABLE1.NAME
FROM TABLE1
WHERE TABLE1.NAME = 'SearchQuery'

UNION

SELECT TABLE2.NAME
FROM TABLE2 
WHERE TABLE2.NAME = 'SearchQuery';

You may want to use UNION ALL if you don't want to exclude repeated values.

To limit your result set you can do something like this:

SELECT * FROM ( HERE GOES ABOVE QUERY ) LIMIT 2;

这篇关于SQL加入两个没有关系的表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆