MYSQL员工和依赖

时间:2013-11-03 06:07:32

标签: mysql sql

我正在使用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还是什么?我不确定如何使中间名匹配工作。

3 个答案:

答案 0 :(得分: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

答案 1 :(得分: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;

答案 2 :(得分: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