3

我在 MYSql 数据库中有一个查询。我有一个表order_det,表的列remarks_desc包含以下条目:

表结构:

Table: order_det

Columns: rec_id, remarks_desc

order_det 表中的示例记录

rec_id      remarks_desc
_________________________________________________________

1           a specific PROGRAMMING problem
2           A software Algorithm
3           software tools commonly USED by programmers
4           Practical, answerable problems that are unique to the programming profession
5           then you’re in the right place to ask your question
6           to see if your QUESTION has been asked BEFORE

我的要求我只想选择包含更多以所有大写字母存储的单词的记录。从以上 6 条记录中,我只想选择以下 1,3,6 条记录:

rec_id      remarks_desc
__________________________________________________
1           a specific PROGRAMMING problem (it contains one all uppercase word PROGRAMMING)
3           software tools commonly USED by programmers (it contains one all uppercase word USED)
6           to see if your QUESTION has been asked BEFORE (it contains two all uppercase words QUESTION and BEFORE)

我尝试使用 LIKE、REGEXP 将其存档,但结果不正确。请帮助我得到正确的结果。

4

2 回答 2

0

Here is the pretty straight forward stored function which returns amount of words in uppercase in row.

Cons:

  • it's stored function not pure SQL;
  • it uses collate
  • it uses regexp, but you can fill free to get rid of it using another inner loop for it;
  • it counts all words but you can add break if you reach 2.

Please find the function on the following link (gist.github.com). It doesn't display correctly here.

于 2012-10-31T11:59:32.050 回答
0

尝试:

SELECT rec_id, remarks_desc FROM order_det WHERE remarks_desc REGEXP '(^|[[:blank:]])[[:upper:]][[:upper:]]+([[:blank:]]|$)'

我假设您要排除单字母大写单词。如果要排除字符串开头的大写单词,则需要调整正则表达式。

确保您的表排序规则区分大小写(_cs 不是 _ci)

我使用了来自http://dev.mysql.com/doc/refman/5.1/en/regexp.html#operator_regexp的信息

但是,如果您必须使用正则表达式从数据库中提取数据,那么值得考虑是否可以改进您的数据库设计。如果您需要数据库的良好性能,这一点尤其重要。

于 2012-10-31T11:57:03.067 回答