简体   繁体   中英

Merging SQL query results

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.

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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM