Implement merging all queries with the inner join

I have 2 tables:

//             table1
+-----------------+-----------------+
|       col1      |        id       |
|-----------------+-----------------|
+-----------------+-----------------+
|       test      |        1        |
|-----------------+-----------------|
|       test      |        2        |
|-----------------+-----------------|
|    anything     |        3        |
|-----------------+-----------------|
|    anything     |        4        |
|-----------------+-----------------|


//             table2
+-----------------+-----------------+
|       col1      |        id       |
|-----------------+-----------------|
+-----------------+-----------------+
|       test      |        5        |
|-----------------+-----------------|
|       test      |        6        |
|-----------------+-----------------|
|    anything     |        7        |
|-----------------+-----------------|
|    anything     |        8        |
|-----------------+-----------------|

When I use union allto get values idwhere col1"test" is equal, the result is required:

select * from table1 where col1='test'
union all
select * from table2 where col1='test'

// the result of this code is: 4 rows. id{1,2,5,6}

Then, for faster and better performance, I implemented it with inner join, but the result is not needed:

select * from table1 t1 inner join table2 t2
on t1.col1=t2.col1
where t1.col1='test'

// the result of this code is: 8 rows. id{1-5,1-6,2-5,2-6}

How can I use inner joinwith these tables to get the result identifier {1, 2, 5, 6}?


EDIT

Example:

table1 {[col1]=word, [col2]=mean}
+-----+------------------------------------------------------------------------------------------+
|  a  | used when referring to someone or something for the first time in a text or conversation |
|-----|------------------------------------------------------------------------------------------|
|  a  | used to indicate membership of a class of people or things                               |
|-----|------------------------------------------------------------------------------------------|
|  x  | xxxxx                                                                                    |
+-----+------------------------------------------------------------------------------------------+

table2 {[col1]=word, [col2]=mean}
+-----+------------------------------------------------------------------------------------------+
|  a  | the blood group whose red cells carry the A antigen                                      |
|-----|------------------------------------------------------------------------------------------|
|  x  | xxxxx                                                                                    |
+-----+------------------------------------------------------------------------------------------+

Now you can use join, and echoit?

a | used when referring to someone or something for the first time in a text or conversation
a | used to indicate membership of a class of people or things
a | the blood group whose red cells carry the A antigen 
+4
source share
1 answer

. , , . , :

SELECT *
FROM table1
JOIN table2 ON table2.col1 = table1.col1 AND table2.col1 = 'test';

:

| col1 | id | col1 | id |
+------+----+------+----+
| test | 1  | test | 5  |
| test | 2  | test | 5  |
| test | 1  | test | 6  |
| test | 2  | test | 6  |

, , , .

, , UNION ALL INNER JOIN . , table1.id table2.id , , .


UNION ALL, . , :

SELECT col1, id
FROM table1
WHERE col1 = 'test'
UNION ALL
SELECT col1, id
FROM table2
WHERE col1 = 'test'

:

| col1 | id |
+------+----+
| test | 1  |
| test | 2  |
| test | 5  |
| test | 6  |

. SQL Fiddle, , .

+3

Source: https://habr.com/ru/post/1589015/


All Articles