1

我正在使用 MYSQL 学习 SQL,并且作为一种练习,我想获取具有受抚养人的员工的姓名,该受抚养人的姓名的第一个字母与该员工的中间名匹配。此外,他们必须是同一性别。到目前为止,我是这样做的:

SELECT
e.fname
, e.minit
, e.lname
FROM
employee e
WHERE EXISTS 
(SELECT * FROM dependent WHERE e.ssn=essn AND e.sex=sex)

现在,我应该使用 LIKE 还是什么?我不确定如何使中间名匹配起作用。

4

3 回答 3

2
select e.fname, e.minit, e.lname, d.name
from employee e
inner join dependent d on e.substring(e.minit, 1, 1) = substring(d.name, 1, 1) 
                       and e.sex = d.sex
于 2013-11-03T06:13:29.647 回答
2
SELECT e.fname, e.minit, e.lname, d.mname  
FROM employee AS e 
JOIN
dependent AS d
ON SUBSTRING(e.fname,1,1) = SUBSTRING(d.mname,1,1) AND e.sex = d.sex;
于 2013-11-03T06:18:31.097 回答
0
 SELECT 
 e.fname
 e.minit
 , e.lname
 , dependent_name
 FROM employee AS e 
 INNER JOIN
 dependent
 ON SUBSTRING(e.minit,1,1) = SUBSTRING(dependent_name,1,1) AND  
 e.sex=dependent.sex AND essn=e.ssn
于 2013-11-03T06:44:11.767 回答