911Rapid
911Rapid

Reputation: 199

MySQL query to join the same table

I want to join two SQL queries run on the same table. Result should contain common rows. I know MySQL does not have INTERSECT. I need to use JOIN I guess, but I am not sure how to when the SQL queries are run on the same table.

A sample query in answer would be great.

Upvotes: 0

Views: 133

Answers (2)

TazGPL
TazGPL

Reputation: 3748

Yes, JOIN is what you're looking for.
In order to refer the same table multiple times you need to use aliases:

SELECT t1.*, t2.*
FROM my_table AS t1
#                ^
#           this is alias
JOIN my_table AS t2 ON t1.id = t2.id
#                ^
#           this is alias

The AS keyword is optional.

Upvotes: 1

Thanos Markou
Thanos Markou

Reputation: 2624

You can use inner or cross join, so something like that:

select a.columnname
from table a
inner join table b
on a.key= b.key
where clauses

Upvotes: 0

Related Questions