Select ALL fields that contains only UPPERCASE let

2020-02-01 03:22发布

How do you select a field that contains only uppercase character in mysql or a field that doesn't contain any lower case character?

标签: mysql
8条回答
一纸荒年 Trace。
2楼-- · 2020-02-01 03:39

Try this -

SELECT * FROM <mytable> WHERE UPPER(<columnname>) = <columnname>
查看更多
孤傲高冷的网名
3楼-- · 2020-02-01 03:45

This worked for me. It found all user emails with uppercase character:

SELECT * FROM users WHERE mail REGEXP BINARY '[A-Z]';
查看更多
兄弟一词,经得起流年.
4楼-- · 2020-02-01 03:47

Found this in the comments - it deserves a post of its own:

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

The problem with WHERE UPPER(mycolumn) = mycolumn is the collation, and it depends on your table what you can use there.

查看更多
We Are One
5楼-- · 2020-02-01 03:51

You may want to use a case sensitive collation. I believe the default is case insensitive. Example:

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');

Then:

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

If you don't want to use a case sensitive collation for the whole table, you can also use the COLLATE clause as @kchau suggested in the other answer.

Let's try with a table using a case insensitive collation:

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');

This won't work very well:

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

But we can use the COLLATE clause to collate the name field to a case sensitive collation:

SELECT * FROM my_table WHERE (name COLLATE latin1_general_cs) REGEXP '^[A-Z]+$';
+------+-----------+
| id   | name      |
+------+-----------+
|    3 | SOMETHING |
+------+-----------+
1 row in set (0.00 sec)
查看更多
够拽才男人
6楼-- · 2020-02-01 03:51

By using REGEXP : http://www.tech-recipes.com/rx/484/use-regular-expressions-in-mysql-select-statements/

Use [:upper:] for uppercase letters.

SELECT * FROM table WHERE field REGEXP '^[[:upper:]+]$'
查看更多
我只想做你的唯一
7楼-- · 2020-02-01 03:52

Basic eg.

SELECT * FROM foo WHERE bar REGEXP '[A-Z]';
查看更多
登录 后发表回答