I’m doing a search in my database. At the same time I have a tagging system installed. Not everything is tagged yet, so I also need the ‘old fashioned’ result from my database. Let me make it clear:
Table A
+----+----------------------------+
| ID | description |
+----+----------------------------+
| 0 | horse going bad |
| 1 | Older Years of Resolutions |
| 2 | The pirate |
| 3 | The Wish list |
| 4 | list that's no list |
+----+----------------------------+
table TAGS
+----+------------+
| ID | tag |
+----+------------+
| 0 | list |
| 1 | knockknock |
+----+------------+
table TAGLINKS
+-------+--------+
| TAGID | JOKEID |
+-------+--------+
| 0 | 2 |
| 0 | 3 |
+-------+--------+
When I do this search:
select * from A where locate('list',description)
I’ll get ID 3 and 4 from table A, which is great.
When I do this search:
select * from tags
join taglinks on tagid=tags.id
join A on A.id=jokeid
where tag='list'
I get ID 2 and 3 from table A.
What I want to get back is ID 2, 3 and 4. So a join of the two results. I tried this:
select * from tags
join taglinks on tagid=tags.id
join A on A.id=jokeid or locate('list',description)
It seems to give me the right result, but it’s so slow it clogs up the server (in reality the tables are MUCH bigger than the examples here). The reason I want a combined query is that I need functions like ORDER BY and LIMIT. So I’m looking to get the combined result from the above two queries.
As Eugen Rieck and Raheel Shan pointed out the answer is a union:
Gives me ID 2, 3 and 4