选择仅包含大写字母的所有字段

时间:2010-10-01 05:48:05

标签: mysql

如何选择mysql中只包含大写字符的字段或不包含任何小写字符的字段?

8 个答案:

答案 0 :(得分:23)

您可能希望使用区分大小写的collation。我相信默认值不区分大小写。例如:

CREATE TABLE my_table (
   id int,
   name varchar(50)
) CHARACTER SET latin1 COLLATE latin1_general_cs;

INSERT INTO my_table VALUES (1, 'SomeThing');
INSERT INTO my_table VALUES (2, 'something');
INSERT INTO my_table VALUES (3, 'SOMETHING');
INSERT INTO my_table VALUES (4, 'SOME4THING');

然后:

SELECT * FROM my_table WHERE name REGEXP '^[A-Z]+$';
+------+-----------+
| id   | name      |
+------+-----------+
|    3 | SOMETHING |
+------+-----------+
1 row in set (0.00 sec)

如果您不想对整个表使用区分大小写的排序规则,您还可以将COLLATE子句用作@kchau suggested in the other answer

让我们尝试使用不区分大小写的排序规则表:

CREATE TABLE my_table (
   id int,
   name varchar(50)
) CHARACTER SET latin1 COLLATE latin1_general_ci;

INSERT INTO my_table VALUES (1, 'SomeThing');
INSERT INTO my_table VALUES (2, 'something');
INSERT INTO my_table VALUES (3, 'SOMETHING');
INSERT INTO my_table VALUES (4, 'SOME4THING');

这不会很好用:

SELECT * FROM my_table WHERE name REGEXP '^[A-Z]+$';
+------+-----------+
| id   | name      |
+------+-----------+
|    1 | SomeThing |
|    2 | something |
|    3 | SOMETHING |
+------+-----------+
3 rows in set (0.00 sec)

但我们可以使用COLLATE子句将名称字段整理为区分大小写的排序规则:

SELECT * FROM my_table WHERE (name COLLATE latin1_general_cs) REGEXP '^[A-Z]+$';
+------+-----------+
| id   | name      |
+------+-----------+
|    3 | SOMETHING |
+------+-----------+
1 row in set (0.00 sec)

答案 1 :(得分:8)

这对我有用。它找到了所有带有大写字符的用户电子邮件:

SELECT * FROM users WHERE mail REGEXP BINARY '[A-Z]';

答案 2 :(得分:5)

SELECT * FROM table1 WHERE (columnname COLLATE latin1_bin )=UPPER(depart);

答案 3 :(得分:3)

试试这个 -

SELECT * FROM <mytable> WHERE UPPER(<columnname>) = <columnname>

答案 4 :(得分:2)

使用REGEXPhttp://www.tech-recipes.com/rx/484/use-regular-expressions-in-mysql-select-statements/

[:upper:]用于大写字母。

SELECT * FROM table WHERE field REGEXP '^[[:upper:]+]$'

答案 5 :(得分:1)

基本eg

SELECT * FROM foo WHERE bar REGEXP '[A-Z]';

答案 6 :(得分:1)

SELECT column_name FROM table WHERE column_name REGEXP BINARY '^[A-Z]+$'

答案 7 :(得分:0)

在评论中找到了它-它值得发表一个帖子:

SELECT * FROM mytable WHERE BINARY mycolumn = BINARY UPPER(mycolumn);

WHERE UPPER(mycolumn) = mycolumn的问题是排序规则,这取决于您的表可以在其中使用什么。