Luis Lavieri
Luis Lavieri

Reputation: 4129

SQL Join two tables without Relations

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?

Upvotes: 5

Views: 17410

Answers (2)

letiagoalves
letiagoalves

Reputation: 11302

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;

Upvotes: 6

Fabian Bigler
Fabian Bigler

Reputation: 10915

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

If they don't share ANY information in the column you want to join, a join is meaningless.


A simple join would look like this:

SELECT Name FROM Table1 t1
JOIN Table2 ON (t1.Name=t2.Name)

Upvotes: 0

Related Questions