简体   繁体   English

SQL是/否查询,没有控制流功能(IF,CASE,COALESCE等)

[英]SQL Yes/No query without control flow functions (IF,CASE,COALESCE etc)

I've been trying to find a way to get a Yes/No answer to a query without using control flow functions(IIF,ELSE,CASE,COALESCE,ISNULL,IFNULL,etc). 我一直在尝试找到一种无需使用控制流功能(IIF,ELSE,CASE,COALESCE,ISNULL,IFNULL等)就可以对查询回答是/否的方法。 I want my query to give me a "Yes" answer if there has been a flight of a certain Airline on a given date between 2 given airports. 我希望我的查询给我一个“是”的答案,如果某特定航空公司在两个给定机场之间的给定日期有航班。 This is what i have done so far 这是我到目前为止所做的

SELECT 'Yes' as Answer
FROM flights
WHERE flights.date = '2014-12-12' AND flights.routes_id IN (SELECT routes.id
                                                            FROM routes
                                                            INNER JOIN airlines
                                                            ON airlines.id = routes.airlines_id
                                                            WHERE airlines_id IN (SELECT airlines.id
                                                                                 FROM airlines
                                                                                 WHERE airlines.name='Olympic Airways')
                                                            AND routes.source_id = (SELECT airports.id
                                                                                    FROM airports
                                                                                    WHERE airports.name like '%Venizelos%')
                                                            AND routes.destination_id=(SELECT airports.id
                                                                                    FROM airports
                                                                                    WHERE airports.name like 'London Gatwick')) 
UNION
SELECT 'No' AS Answer
FROM flights
WHERE flights.date = '2014-12-12' AND flights.routes_id  NOT IN (SELECT routes.id
                                                                FROM routes
                                                                INNER JOIN airlines
                                                                ON airlines.id = routes.airlines_id
                                                                WHERE airlines_id IN (SELECT airlines.id
                                                                                    FROM airlines
                                                                                    WHERE airlines.name = 'Olympic Airways')
                                                                AND routes.source_id = (SELECT airports.id
                                                                                    FROM airports
                                                                                    WHERE airports.name like '%Venizelos%')
                                                                AND routes.destination_id=(SELECT airports.id
                                                                                    FROM airports
                                                                                    WHERE airports.name like 'London Gatwick'))

The problem is that when i run the queries it prints both Yes and No, when it should print only one of those two. 问题是,当我运行查询时,它会同时打印“是”和“否”,而当它只打印这两个之一时。 How can it be fixed? 如何解决?

This happens because in that day there will be flight for that airline, but also for others, from/to that airports but also from others. 发生这种情况的原因是,那天将有该航空公司的航班飞往其他机场,也有其他航空公司的航班往返该机场。

Put your query inside another query like: 将您的查询放在另一个查询中,例如:

SELECT Max(Answer)
from (
      your query from the question
)

This will return Yes if you have at leas a good flight (because the internal query will return Yes and No ) and No if you have only no matches (the results from the internal query will be only No ). 这将返回Yes ,如果你有在执法机关良好的飞行(因为内部查询将返回YesNo ),并No如果只有不匹配(从内部查询的结果将是只No )。

Hope this helps. 希望这可以帮助。

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

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