The EMPLOYEES table contains these columns:
EMPLOYEE_ID NUMBER(4)
LAST_NAME VARCHAR2 (25)
JOB_ID VARCHAR2(10)
You want to search for strings that contain 'SA_' in the JOB_ID column. Which SQL statement do you
use?
A. SELECT employee_id, last_name, job_id
FROM employees
WHERE job_id LIKE '%SA\_%' ESCAPE '\';
B. SELECT employee_id, last_name, job_id
FROM employees
WHERE job_id LIKE '%SA_%';
C. SELECT employee_id, last_name, job_id
FROM employees
WHERE job_id LIKE '%SA_' ESCAPE "\";
D. SELECT employee_id, last_name, job_id
FROM employees
WHERE job_id = '%SA_';
Answer: A
为什么答案是A呢?B不行吗?
------解决方案--------------------------------------------------------
B不行。在LIKE中,_表示匹配任意单个字符,%表示匹配任意单个或多个字符
所以B中LIKE '%SA_%'表示就是含SA字符的JOB_ID,而不是sa_了。
------解决方案--------------------------------------------------------
'_'在oracle中是通配符,现在又要求查找的字符串中含'_',所以加个ESCAPE \,表示不将ESCAPE \后的字符做oracle关键字处理。
网上资料很详细的。
------解决方案--------------------------------------------------------
哈哈,没事干。看着分不少,来接点分。
------解决方案--------------------------------------------------------
A正确!
验证一下不就知道了吗?
SQL> select * from emp where job like '%E_%';
EMPNO ENAME JOB MGR HIREDATE SAL COMM
---------- ---------- -------------------- ---------- ---------- ---------- ---------- -
7369 SMITH CLERK 7902 17-12月-80 800
7499 ALLEN SALESMAN 7698 20-2月 -81 1600 300
7521 WARD SALESMAN 7698 22-2月 -81 1250 500
7566 JONES MANAGER 7839 02-4月 -81 2975
7654 MARTIN SALESMAN 7698 28-9月 -81 1250 1400