0

我是 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 年以后出生并在英国和西班牙工作的医生的姓名和身份证是什么?谢谢

4

2 回答 2

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;
于 2012-11-23T20:27:12.563 回答
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');

我希望这已经足够了。

于 2012-11-23T20:30:37.313 回答