SQL DML查询 - 需要帮助

时间:2012-11-23 20:22:29

标签: sql dml

我是SQL的初学者,我正在尝试创建一个查询。

我有这些表格:

CREATE TABLE Hospital (
    hid INT PRIMARY KEY,
    name VARCHAR(127) UNIQUE,
    country VARCHAR(127),
    area INT
);
CREATE TABLE Doctor (
    ic INT PRIMARY KEY,
    name VARCHAR(127),
    date_of_birth INT,
);
CREATE TABLE Work (
    hid INT,
    ic INT,
    since INT,
    FOREIGN KEY (hid) REFERENCES Hospital (hid),
    FOREIGN KEY (ic) REFERENCES Doctor (ic),
    PRIMARY KEY (hid,ic)
 );

查询是:1980年以后出生并在英格兰和西班牙工作的医生的姓名和IC是什么?感谢

2 个答案:

答案 0 :(得分:1)

你可以使用类似的东西:

select d.name
from doctor d
left join work w
  on d.ic = w.ic
left join hospital h
  on w.hid = h.id
where d.date_of_birth >= '1980-01-01'
  and h.country in ('England', 'Spain')
group by d.name
having count(distinct h.country) = 2;

答案 1 :(得分:0)

试试这个:

SELECT d.name, d.ic
FROM doctor d
JOIN work w ON d.ic = i.ic
JOIN hospital h ON w.hid = h.hid
WHERE YEAR(d.date_of_birth) >= 1980
AND UPPER(h.country) IN ('ENGLAND', 'SPAIN');

我希望这已经足够了。