我有如下表格:
CREATE TABLE SKILL(
sname VARCHAR(30) NOT NULL,
CONSTRAINT SKILL_pkey PRIMARY KEY ( sname ) );
CREATE TABLE APPLICANT(
anumber DECIMAL(6) NOT NULL,
fname VARCHAR(20) NOT NULL,
lname VARCHAR(30) NOT NULL,
dob DATE NOT NULL,
city VARCHAR(30) NOT NULL,
state VARCHAR(20) NOT NULL,
phone DECIMAL(10) NOT NULL,
fax DECIMAL(10) ,
email VARCHAR(50) ,
CONSTRAINT APPLICANT_pkey PRIMARY KEY ( anumber ) );
CREATE TABLE SPOSSESSED(
anumber DECIMAL(6) NOT NULL,
sname VARCHAR(30) NOT NULL,
slevel DECIMAL(2) NOT NULL,
CONSTRAINT SPOSSESSED_pkey PRIMARY KEY ( anumber, sname ),
CONSTRAINT SPOSSESSED_fkey1 FOREIGN KEY ( anumber )
REFERENCES APPLICANT ( anumber )
ON DELETE CASCADE,
CONSTRAINT SPOSSESSED_fkey2 FOREIGN KEY ( sname )
REFERENCES SKILL ( sname ),
CONSTRAINT SPOSSESSED_check1 CHECK ( slevel IN
( 1,2,3,4,5,6,7,8,9,10 ) ) );
我被命令:
创建一个关系表,其中包含有关所有技能的名称和申请人拥有的最大技能水平的信息,以及拥有最高级别技能的申请人。忽略任何申请人不具备的技能。必须通过创建表的同一SQL语句将所有数据加载到表中。加载数据后,强制执行适当的主键和参照完整性约束(如果有)。
根据我的理解,我想出了两个独立的脚本来实现首选:
SELECT * FROM SKILL;
SELECT MAX(SPOSSESSED.slevel), APPLICANT.anumber
FROM APPLICANT RIGHT OUTER JOIN SPOSSESSED
ON APPLICANT.anumber = SPOSSESSED.anumber
GROUP BY anumber;
我不知道我的选择是否合适?有人可以帮忙吗?我读的要求越多,我就越感到困惑。
编辑#1:
示例数据 技能:
INSERT INTO SKILL VALUES ( 'C++ programming' );
INSERT INTO SKILL VALUES ( 'C programming' );
INSERT INTO SKILL VALUES ( 'Java programming' );
INSERT INTO SKILL VALUES ( 'SQL programming' );
INSERT INTO SKILL VALUES ( 'driving' );
INSERT INTO SKILL VALUES ( 'painting' );
INSERT INTO SKILL VALUES ( 'cooking' );
申请人:
INSERT INTO APPLICANT VALUES ( 000001, 'Harry', 'Potter', '1980-12-12', 'Perth', 'Western Australia', 645278453, NULL, 'jones@gmail.com' );
INSERT INTO APPLICANT VALUES ( 000002, 'Johnny', 'Walker', '1990-02-13', 'Geelong', 'Victoria', 63569784, 63569785, 'blunder@hotmail.com' );
INSERT INTO APPLICANT VALUES ( 000003, 'Mary', 'Poppins', '1950-01-01', 'Melbourne', 'Victoria', 62389541, NULL, NULL );
INSERT INTO APPLICANT VALUES ( 000004, 'Michael', 'Collins', '1960-05-25', 'Brisbane', 'Queensland', 63336666, NULL, 'mike@hotmail.com');
INSERT INTO APPLICANT VALUES ( 000005, 'Margaret', 'Finch', '1953-12-07', 'Sydney','New South Wales', 64573489, NULL, 'mf@163.com');
INSERT INTO APPLICANT VALUES ( 000006, 'Claudia', 'Kowalewski', '1959-05-03', 'Hobart', 'Tasmania', 64577744, NULL, 'cch@cs.odmg.org');
INSERT INTO APPLICANT VALUES ( 000007, 'James', 'Bond', '1960-06-01','Perth', 'Western Australia', 645278434, NULL, 'james@bigpond.com');
SPOSSESSED:
INSERT INTO SPOSSESSED VALUES ( 000001, 'Java programming', 9 );
INSERT INTO SPOSSESSED VALUES ( 000001, 'C programming', 4 );
INSERT INTO SPOSSESSED VALUES ( 000001, 'cooking', 9 );
INSERT INTO SPOSSESSED VALUES ( 000002, 'Java programming', 9 );
INSERT INTO SPOSSESSED VALUES ( 000002, 'driving', 9 );
INSERT INTO SPOSSESSED VALUES ( 000003, 'C++ programming', 10 );
INSERT INTO SPOSSESSED VALUES ( 000003, 'Java programming', 9 );
INSERT INTO SPOSSESSED VALUES ( 000003, 'painting', 5 );
INSERT INTO SPOSSESSED VALUES ( 000005, 'SQL programming', 6 );
INSERT INTO SPOSSESSED VALUES ( 000006, 'SQL programming', 8 );
INSERT INTO SPOSSESSED VALUES ( 000007, 'SQL programming', 9 );
INSERT INTO SPOSSESSED VALUES ( 000007, 'cooking', 10 );
我真的不明白我需要做什么,所以我请你看看你怎么解释它。
但是根据我的理解,它应该会显示一张带有sname的表格,这个sname的最高级别以及那个sname中最高级别的aumber。
这是具体的我可以去......
答案 0 :(得分:1)
以下查询将创建所有技能的列表,以及每项技能技能水平最高的申请人。
SELECT
c.`anumber`,
a.`sname`
FROM `SPOSSESSED` a
JOIN (
SELECT
`sname`
MAX(`slevel`) as `slevel`
FROM `SPOSSESSED`
GROUP BY `sname`
) b
ON b.`sname` = a.`sname` AND b.`slevel` = a.`slevel`
JOIN `APPLICANT` c
ON c.`anumber` = a.`anumber`
GROUP BY a.`sname`, c.`anumber`
如果不是每种技能技能水平最高的申请人名单,您需要计算每种技能技能水平最高的申请人, 你可以试试这个:
SELECT
a.`sname`,
a.`slevel`,
count(DISTINCT c.`anumber`) as `numapplicants`
FROM `SPOSSESSED` a
JOIN (
SELECT
`sname`
MAX(`slevel`) as `slevel`
FROM `SPOSSESSED`
GROUP BY `sname`
) b
ON b.`sname` = a.`sname` AND b.`slevel` = a.`slevel`
JOIN `APPLICANT` c
ON c.`anumber` = a.`anumber`
GROUP BY a.`sname`, a.`slevel`
未经测试,可能有拼写错误。