Reputation: 305
I'm a walkthrough author for games. I am trying to display all rows that match all criterias. Currently, the table displays results from each of the tables but I want to merge these results so that if the search criteria matches all that the user input, it only returns rows that match it. What's the best way to achieve this? I was trying to use AND before the UNION clauses but it gives me error #1064.
Let's have this sample table.
+--------+-------------------------+------------+--------------+
| FAQ_ID | FAQ_Title | Game | Platforms |
+--------+-------------------------+------------+--------------+
| 32 | General Walkthrough FAQ | WWE | PS2, PSP, DS |
| 34 | Early EXP Farming FAQ | Digimon | PS4, PS Vita |
| 35 | General Walkthrough FAQ | Toy Story | PS1 |
| 36 | General Walkthrough FAQ | Metal Slug | DS |
+--------+-------------------------+------------+--------------+
Then I have this code
SELECT FAQ_ID, FAQ_Title, Game, Platforms FROM faqlist WHERE FAQ_Title
LIKE 'General Walkthrough%'
AND UNION
SELECT FAQ_ID, FAQ_Title, Game, Platforms FROM faqlist WHERE Platforms = '%DS%'
AND UNION
SELECT FAQ_ID, FAQ_Title, Game, Platforms FROM faqlist WHERE Game = 'Metal Slug'
ORDER BY FAQ_ID ASC;
This should only return the row with FAQ_ID of 36 and eliminates the rest.
Upvotes: 0
Views: 33
Reputation: 15057
You can also use the OR or AND Condition like this:
SELECT FAQ_ID, FAQ_Title, Game, Platforms
FROM faqlist
WHERE
FAQ_Title LIKE 'General Walkthrough%'
OR
Platforms = '%DS%'
OR
Game = 'Metal Slug'
ORDER BY FAQ_ID ASC;
Upvotes: 1