大家好,这是个问题:
显示电子邮件地址中包含全名的员工的员工 ID 和电子邮件。
你觉得我可以用什么?我可以使用 substr 吗?如果没有,还有其他建议吗?谢谢各位平安!
例如,我会使用正则表达式
CREATE TABLE employees (
emp_id NUMBER PRIMARY KEY,
first_name VARCHAR2(30),
last_name VARCHAR2(30),
email VARCHAR2(255)
);
INSERT INTO employees VALUES (1, 'Joseph', 'Ratzinger', 'benedictxvi@vatican.va');
INSERT INTO employees VALUES (2, 'Tim', 'Cook', 'tcook@apple.com');
INSERT INTO employees VALUES (3, 'Larry', 'Ellison', 'Ellison@oracle.com');
INSERT INTO employees VALUES (4, 'Michael', 'Dell', 'Michael@dell.com');
SELECT emp_id, email FROM employees WHERE regexp_like(email, last_name);
3 Ellison@oracle.com
要包括与案例无关的结果,请使用:
SELECT emp_id, email FROM employees WHERE regexp_like(email, last_name, 'i');
2 tcook@apple.com
3 Ellison@oracle.com
4 Michael@dell.com
如果您需要排除主机名,请将“@”附加到模式...
SELECT emp_id, email FROM employees WHERE regexp_like(email, last_name||'@', 'i');
2 tcook@apple.com
3 Ellison@oracle.com
...或从电子邮件中提取主机名:
SELECT emp_id, email
FROM employees
WHERE regexp_like(regexp_substr(email,'.*@'), last_name, 'i');