有什么方法可以改变这个SQL查询,以便它不返回包含空格的任何结果?

时间:2013-07-03 14:42:15

标签: mysql sql

我有一个用于搜索功能的SQL查询(如下所示)

SELECT t.op_id, t.op_desc FROM operator
WHERE UPPER(t.op_id) LIKE UPPER(?);

当我的网页首次加载时,它只返回表格中的每个操作符。

数据库中有一些条目,其中运算符id(t.op_id)包含一些空格“Operator A”。有什么办法可以改变这个查询,以便排除运算符名称中包含空格的结果吗?

4 个答案:

答案 0 :(得分:4)

要忽略空格,可以尝试

WHERE NOT LIKE '% %'

perform the sameINSTR

答案 1 :(得分:1)

你可以试试这个:

SELECT t.op_id, t.op_desc FROM operator
WHERE t_op_id not like '% %'

答案 2 :(得分:1)

您可以使用trim功能。我假设您的运营商名称列为op_name。请尝试以下方法:

SELECT t.op_id, t.op_desc FROM operator
WHERE UPPER(t.op_id) LIKE UPPER(?) and instr(trim(t.op_name), ' ') <= 0;

答案 3 :(得分:1)

您也可以尝试:

where INSTR(t_op_id,' ') <= 0