实现条件mysql where子句的最佳方法

时间:2012-08-16 07:38:46

标签: mysql where-clause

Helllo伙计们,

我有一个包含条目的表,这些条目具有字段机密性(公共,机密)和字段类型(项目,海报,出版物......) 。 那么我想做什么:我想在SELECT中过滤所有条目,但不要过滤具有指定类型和机密性的条目。

例如:

type=project, confidentiality=public       | should be selected
type=project, confidentiality=confidential | should be not selected
type=poster, confidentiality=public        | should be selected
type=poster, confidentiality=confidential  | should be selected

有没有好办法呢?

SELECT ...
WHERE (type = 'project' AND confidentiality = 'public') OR type = 'publication' OR type
= 'poster' OR type = 'lecture' OR type = 'committee'
GROUP BY

提前致谢

3 个答案:

答案 0 :(得分:1)

根据您提供的信息,您的查询看起来没问题。

这个较短的查询会起作用吗?

SELECT ...
WHERE (type = 'project' AND confidentiality = 'public')
OR type <> 'project'

注意:上述查询可缩短为:

SELECT ...
WHERE confidentiality = 'public'
OR type <> 'project'

答案 1 :(得分:1)

在您的示例中,您要选择confidentiality不是机密的记录。对?试试这个,

SELECT ....
FROM ....
WHERE type <> 'project' AND confidentiality <> 'confidential'

OR与

相同
SELECT ....
FROM ....
WHERE NOT (type = 'project' AND confidentiality = 'confidential')

答案 2 :(得分:0)

您的预期逻辑似乎是:

SELECT ... WHERE type != 'project' OR confidentiality = 'public';
相关问题