选择带OR和NAND的查询

时间:2013-06-05 12:51:37

标签: mysql sql operators logic

我正在尝试使用OR和NAND操作设计查询,有人可以告诉我查询是否正确:

问题1:此查询是否正确?

select * from country where country_code='AL' UNION (SELECT * from country
WHERE country_id NOT IN
       (SELECT country_id from country
              where country_name='Land islands'
        UNION
        SELECT country_id from country
               where country_code='AX'
        ));

我借助我的另一个问题创建了查询;

Ref:

问题2:

我想在它们之间使用AND,NOT,NAND,OR操作的5个查询。 我创建了3个(检查我的问题1)我需要帮助来创建5个选择查询。

我的应用程序是搜索应用程序,用户可以在每个块之间使用i5查询块搜索数据库,我们有如下所述的操作。但是这些操作是在一张表中进行的。

说明:

我想在表格中选择记录,我将在表格之间使用许多操作。 MAX 5在一边查询操作。

查询1,查询2,查询3,查询4,查询5.

案例:

(Query 1 AND Query 2 OR  Query 3 NAND Query 4 NOT Query 5)
(Query 1 OR Query 2 OR  Query 3 OR Query 4 OR Query 5)
(Query 1 AND Query 2 AND Query 3 AND Query 4 AND Query 5)
(Query 1 NOT Query 2 NOT Query 3 NOT Query 4 NOT Query 5)

等。查询块之间的操作可能会有所不同......我会根据这个编写我的PHP脚本,但我想以这样的方式执行逻辑,我可以加入每个逻辑..

现在我需要帮助

2 个答案:

答案 0 :(得分:2)

为什么您在同一UNION之间使用Table。您可以使用operators之类的

轻松完成此查询
Select * from country where country_code='AL' or (country_name <> 'Land islands' and country_code <> 'AX');

希望它适合你。

答案 1 :(得分:2)

编写第一个查询的最简单方法:

select *
from country
where country_code='AL' and country_name not in ('Land islands', 'AX')

这可以假设country_idcountry表中是唯一的 - 基于命名的合理假设。如果不是这种情况,查询会有点复杂。

您的原始查询需要对表进行多次扫描,NOT IN使用子查询进行反加入,并使用聚合删除重复项(它包含union而不是union all)。

修订版只扫描表,保留与where子句匹配的行。

根据您的第一个查询,您可以在{{1}中使用ANDOR(可能还有INNOT IN)来完成您想要的一切条款。

相关问题