0
我很難實現這些查詢。有人可以幫忙嗎?MySQL表查找查詢
查詢1:查找在同一分店至少參加過兩次考試的駕駛員姓名。
查詢2:查找當他/她參加更多考試時考試分數變低的駕駛員的姓名。
我的代碼
create table branch(branch_id integer, branch_name varchar(20), branch_addr varchar(50), branch_city varchar(20), branch_phone integer);
create table driver(driver_ssn integer, driver_name varchar(20), driver_addr varchar(50), driver_city varchar(20), driver_birthdate date, driver_phone integer);
create table license(license_no integer, driver_ssn integer, license_type char, license_class integer, license_expiry date, issue_date date, branch_id integer);
create table exam(driver_ssn integer, branch_id integer, exam_date date, exam_type char, exam_score integer);
insert into branch values(10,'Main','1234 Main St.','Vancouver',5551234);
insert into branch values(20,'Richmond','23 No. 3 road','Richmond',5552331);
insert into branch values(30,'West Creek','251 creek rd.','Sechelt',5552511);
insert into branch values(40,'Blenheim','1342 W.22 Ave.','Burnaby',5551342);
insert into driver values(11111111, 'Bob Smith','111 E.11 st.', 'Vancouver', '1975-01-01',5551111);
insert into driver values(22222222, 'John Walters','222 E.22 st.', 'Burnaby', '1976-02-02',5552222);
insert into driver values(33333333, 'Troy Rops','333 W.33 ave.', 'Richmond', '1970-03-03',5553333);
insert into driver values(44444444, 'Kevin Mark','444 E.4 ave.', 'Vancouver', '1974-04-04',5554444);
insert into license values(1, 11111111,'D', 5, '1999-05-25','1997-05-25',20);
insert into license values(2, 22222222,'D', 5, '1998-08-29','1996-08-29',40);
insert into license values(3, 33333333,'L', 5, '1997-12-27','1997-06-27',20);
insert into license values(4, 44444444,'D', 5, '1999-08-30','1997-08-30',40);
insert into exam values(11111111,20, '1997-05-25', 'D',79);
insert into exam values(11111111,20, '1997-12-02', 'L',97);
insert into exam values(22222222,30, '1996-05-06', 'L',25);
insert into exam values(22222222,40, '1996-06-10', 'L',51);
insert into exam values(22222222,40, '1996-08-29', 'D',81);
insert into exam values(33333333,10, '1997-07-07', 'L',45);
insert into exam values(33333333,20, '1997-06-27', 'L',49);
insert into exam values(33333333,20, '1997-07-27', 'L',61);
insert into exam values(44444444,10, '1997-07-27', 'L',71);
insert into exam values(44444444,20, '1997-08-30', 'D',65);
我上查詢1嘗試:
select driver.driver_name
from exam, branch, driver
where driver.driver_ssn = exam.driver_ssn AND
(select exam.driver_ssn
from exam
group by exam.driver.ssn, exam.branch_id
having count(exam.branch_id) >= 2);
家庭作業?也這麼覺得。他們在教JOIN之前教過子查詢嗎?這很糟糕。 – Strawberry
@Strawberry他們教過的連接,但不好。我試圖自己弄清楚 – user1681664