MySQL表查找查询

时间:2013-11-14 17:02:18

标签: mysql sql

我很难实现这些查询。有人可以帮忙。

查询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);

2 个答案:

答案 0 :(得分:0)

嗯,第一个查询可能看起来像这样......

SELECT something
  FROM somehere d 
  JOIN somewhere_else e
    ON e.some_other_thing = d.some_other_thing
 GROUP 
    BY d.some_other_thing
     , e.and_yet_another_thing
HAVING COUNT(*) >= 2;

假设问题2意味着'找到后续考试成绩低于前一成绩的司机的名字',那么这可能是这样的......

SELECT DISTINCT d.driver_name
           FROM ... d
           JOIN ... preceding
             ON preceding.... = d....
           JOIN ... subsequent
             ON subsequent.... = preceding....
            AND subsequent.... > preceding....
            AND subsequent.... < preceding....;

答案 1 :(得分:0)

first query something like this

select d.driver_name from driver d inner join exam e on d.driver_ssn=e.driver_ssn  inner join branch b on e.branch_id=b.branch_id

group by d.driver_name
having count(*)>=2