简体   繁体   English

合并SQL查询结果

[英]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. 我试图在UNION子句之前使用AND,但它给我错误#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. 这只会返回FAQ_ID为36的行,并消除其余的行。

You can also use the OR or AND Condition like this: 您还可以使用OR或AND条件,如下所示:

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;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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