MYSQL问题 - >在哪里......或者

时间:2011-03-22 20:27:43

标签: mysql

感谢这个网站上的一些帮助,我写了一个查询来查找WHERE'first'或'last'没有大写的任何行。每个条件本身都可以正常工作,但当使用OR组合成单个查询时,我不再检测“第一”列中的非大写条目 - 仅在“最后”列中。我哪里错了?感谢。

SELECT first,last FROM main WHERE 

CONCAT( UPPER( SUBSTRING(first,1,1) ), SUBSTRING(first FROM 2) ) != first

OR

CONCAT( UPPER( SUBSTRING(last,1,1) ),  SUBSTRING(last FROM 2) ) != last

COLLATE latin1_general_cs

2 个答案:

答案 0 :(得分:6)

你需要两个条件下的COLLATE:

SELECT first,last FROM main WHERE 
CONCAT( UPPER( SUBSTRING(first,1,1) ), SUBSTRING(first FROM 2) ) != first
COLLATE latin1_general_cs
OR
CONCAT( UPPER( SUBSTRING(last,1,1) ),  SUBSTRING(last FROM 2) ) != last
COLLATE latin1_general_cs

或者,根据上述评论中的建议简化查询:

SELECT first,last FROM main WHERE 
UPPER( SUBSTRING(first,1,1))  != SUBSTRING(first,1,1)
COLLATE latin1_general_cs
OR
UPPER( SUBSTRING(last,1,1)) != SUBSTRING(last,1,1)
COLLATE latin1_general_cs

答案 1 :(得分:1)

尝试通过添加额外的括号来更明确地使WHERE子句的评估顺序...

SELECT first,last FROM main WHERE 

(CONCAT( UPPER( SUBSTRING(first,1,1) ), SUBSTRING(first FROM 2) ) != first)

OR

(CONCAT( UPPER( SUBSTRING(last,1,1) ),  SUBSTRING(last FROM 2) ) != last)

COLLATE latin1_general_cs