为什么 Oracle XE 数据库中的 WHERE 子句在与字符串进行比较时不起作用?
Why the WHERE clause in Oracle XE database doesn't work when comparing with a string?
我有一个名为 MEDECIN
的 table,其中包含 2 列,如下所示:
SQL> DESC MEDECIN;
Name Null? Type
----------------------------------------- -------- ----------------------------
NUM_MED NOT NULL NUMBER(4)
SPECIALITE NOT NULL NVARCHAR2(13)
它包含 32 行,这是它的内容:
SQL> SELECT * FROM MEDECIN;
NUM_MED SPECIALITE
---------- ----------------------------------------------------
4 Orthopédiste
7 Cardiologue
8 Cardiologue
10 Cardiologue
19 Traumatologue
24 Orthopédiste
26 Orthopédiste
27 Orthopédiste
31 Anesthésiste
34 Pneumologue
50 Pneumologue
53 Traumatologue
54 Pneumologue
64 Radiologue
80 Cardiologue
82 Orthopédiste
85 Anesthésiste
88 Cardiologue
89 Radiologue
99 Anesthésiste
113 Pneumologue
114 Traumatologue
122 Pneumologue
126 Radiologue
135 Anesthésiste
140 Cardiologue
141 Traumatologue
144 Radiologue
152 Cardiologue
179 Anesthésiste
180 Cardiologue
196 Traumatologue
32 rows selected.
问题是当我执行请求时 SELECT * FROM MEDECIN WHERE SPECIALITE = 'Cardiologue';
我得到 no rows selected
!怎么会这样?如您所见,有很多行 SPECIALITE = 'Cardiologue'
.
尝试
SELECT * FROM MEDECIN WHERE to_char(SPECIALITE) = 'Cardiologue'
因为它是一个 nvarchar2 尝试在字符串前面放一个大写字母 N 就像这样 = N'Cardiologue'。
应该可以,除非过滤器无法匹配任何行。
设置
SQL> CREATE TABLE MEDECIN
2 (
3 NUM_MED NUMBER(4) NOT NULL,
4 SPECIALITE NVARCHAR2(13) NOT NULL
5 );
Table created.
SQL> INSERT INTO MEDECIN VALUES
2 (4, 'Orthopédiste'
3 );
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM medecin;
NUM_MED SPECIALITE
---------- -------------
4 Orthopédiste
查询
SQL> SELECT * FROM MEDECIN WHERE SPECIALITE = 'Orthopédiste';
NUM_MED SPECIALITE
---------- -------------
4 Orthopédiste
您也可以尝试 TRIM/LIKE 删除任何尾随空格。
例如,
SQL> INSERT INTO MEDECIN VALUES
2 (5, 'Orthopédis '
3 );
1 row created.
SQL> SELECT * FROM MEDECIN WHERE SPECIALITE = 'Orthopédis';
no rows selected
SQL> SELECT * FROM MEDECIN WHERE SPECIALITE LIKE 'Orthopédis%';
NUM_MED SPECIALITE
---------- -------------
4 Orthopédiste
5 Orthopédis
SQL> SELECT * FROM MEDECIN WHERE TRIM(SPECIALITE) = 'Orthopédiste';
NUM_MED SPECIALITE
---------- -------------
4 Orthopédiste
我有一个名为 MEDECIN
的 table,其中包含 2 列,如下所示:
SQL> DESC MEDECIN; Name Null? Type ----------------------------------------- -------- ---------------------------- NUM_MED NOT NULL NUMBER(4) SPECIALITE NOT NULL NVARCHAR2(13)
它包含 32 行,这是它的内容:
SQL> SELECT * FROM MEDECIN; NUM_MED SPECIALITE ---------- ---------------------------------------------------- 4 Orthopédiste 7 Cardiologue 8 Cardiologue 10 Cardiologue 19 Traumatologue 24 Orthopédiste 26 Orthopédiste 27 Orthopédiste 31 Anesthésiste 34 Pneumologue 50 Pneumologue 53 Traumatologue 54 Pneumologue 64 Radiologue 80 Cardiologue 82 Orthopédiste 85 Anesthésiste 88 Cardiologue 89 Radiologue 99 Anesthésiste 113 Pneumologue 114 Traumatologue 122 Pneumologue 126 Radiologue 135 Anesthésiste 140 Cardiologue 141 Traumatologue 144 Radiologue 152 Cardiologue 179 Anesthésiste 180 Cardiologue 196 Traumatologue 32 rows selected.
问题是当我执行请求时 SELECT * FROM MEDECIN WHERE SPECIALITE = 'Cardiologue';
我得到 no rows selected
!怎么会这样?如您所见,有很多行 SPECIALITE = 'Cardiologue'
.
尝试
SELECT * FROM MEDECIN WHERE to_char(SPECIALITE) = 'Cardiologue'
因为它是一个 nvarchar2 尝试在字符串前面放一个大写字母 N 就像这样 = N'Cardiologue'。
应该可以,除非过滤器无法匹配任何行。
设置
SQL> CREATE TABLE MEDECIN
2 (
3 NUM_MED NUMBER(4) NOT NULL,
4 SPECIALITE NVARCHAR2(13) NOT NULL
5 );
Table created.
SQL> INSERT INTO MEDECIN VALUES
2 (4, 'Orthopédiste'
3 );
1 row created.
SQL> COMMIT;
Commit complete.
SQL> SELECT * FROM medecin;
NUM_MED SPECIALITE
---------- -------------
4 Orthopédiste
查询
SQL> SELECT * FROM MEDECIN WHERE SPECIALITE = 'Orthopédiste';
NUM_MED SPECIALITE
---------- -------------
4 Orthopédiste
您也可以尝试 TRIM/LIKE 删除任何尾随空格。
例如,
SQL> INSERT INTO MEDECIN VALUES
2 (5, 'Orthopédis '
3 );
1 row created.
SQL> SELECT * FROM MEDECIN WHERE SPECIALITE = 'Orthopédis';
no rows selected
SQL> SELECT * FROM MEDECIN WHERE SPECIALITE LIKE 'Orthopédis%';
NUM_MED SPECIALITE
---------- -------------
4 Orthopédiste
5 Orthopédis
SQL> SELECT * FROM MEDECIN WHERE TRIM(SPECIALITE) = 'Orthopédiste';
NUM_MED SPECIALITE
---------- -------------
4 Orthopédiste