SELECT行不以特定字符结尾

时间:2016-02-17 08:57:42

标签: mysql

我有一张问题表。我需要找到有'?'在问题文本中因为不良的字符编码/整理。我需要查找所有包含'?'但也需要忽略问题末尾的问号。我尝试了这个查询,但我仍然在问题的末尾找到带有问号的行

SELECT * 
FROM `kc_questions` 
WHERE `question` LIKE "%?%" /* WHICH CONTAINS '?' */
AND `question` NOT LIKE "%?"  /* DOES NOT END WITH '?' */

编辑:phpmyadmin实际上告诉我查询有问题: enter image description here

然而,查询成功地返回以'?'。

结尾的行

2 个答案:

答案 0 :(得分:2)

根据示例数据,我尝试了以下演示,它按预期工作。

SQL:

create table kc_questions(question varchar(200));
insert into kc_questions values
('Ex1. ?-particles are harmul for human body. Select True or False.'),
('Ex2. What is your name?');

SELECT question FROM kc_questions;
SELECT * 
FROM `kc_questions` 
WHERE `question` LIKE "%?%" 
AND `question` NOT LIKE "%?";

输出:

mysql> SELECT question FROM kc_questions;
+-------------------------------------------------------------------+
| question                                                          |
+-------------------------------------------------------------------+
| Ex1. ?-particles are harmul for human body. Select True or False. |
| Ex2. What is your name?                                           |
+-------------------------------------------------------------------+
2 rows in set (0.00 sec)

mysql> SELECT *
    -> FROM `kc_questions`
    -> WHERE `question` LIKE "%?%"
    -> AND `question` NOT LIKE "%?";
+-------------------------------------------------------------------+
| question                                                          |
+-------------------------------------------------------------------+
| Ex1. ?-particles are harmul for human body. Select True or False. |
+-------------------------------------------------------------------+
1 row in set (0.00 sec)
mysql> select version();
+-----------+
| version() |
+-----------+
| 5.7.8-rc  |
+-----------+
1 row in set (0.00 sec)

答案 1 :(得分:2)

您可以使用正则表达式

SELECT * 
FROM `kc_questions` 
WHERE `question` REGEXP '.*\?.+$'

基本上,您搜索的问题包含'?'在'?'

之后至少有一个字符