在MYSQL中选择特定客户?

时间:2016-05-06 22:40:14

标签: mysql sql

我创建了以下表格:

create table customers

(
        ID             varchar(9),
        name           varchar(15),  

CONSTRAINT pk_id PRIMARY KEY (ID)
);


create table living_places
(
        code     varchar(7),
        ID      varchar(9),

CONSTRAINT pk_code PRIMARY KEY (code)
);

create table policies
(
        code_policy         varchar(7),
        code_living_place       varchar(7),

CONSTRAINT pk_code_policy PRIMARY KEY (code_policy)
);

create table bills
(
        code          varchar(7),
        code_policy   varchar(7),
        paid_out      boolean,

CONSTRAINT pk_code_bill PRIMARY KEY (code)
);
I inserted the following dates:

我插入了以下日期:

insert into customers(ID, name) 
values('fx1','Louis');
insert into customers(ID, name) 
values('fx2','Peter');
insert into customers(ID, name) 
values('fx3','Alice');

insert into living_places(code, ID) 
values('001','fx1');
insert into living_places(code, ID) 
values('002','fx2');
insert into living_places(code, ID) 
values('003','fx1');
insert into living_places(code, ID) 
values('004','fx3');

insert into policies(code_policy, code_living_place) 
values('p1','001');
insert into policies(code_policy, code_living_place) 
values('p2','002');
insert into policies(code_policy, code_living_place) 
values('p3','003');

insert into bills(code, code_policy, paid_out) 
values('b1','p1','1');
insert into bills(code, code_policy, paid_out) 
values('b2','p1','1');
insert into bills(code, code_policy, paid_out) 
values('b3','p2','0');
insert into bills(code, code_policy, paid_out) 
values('b4','p2','1');
insert into bills(code, code_policy, paid_out) 
values('b5','p3','0');
insert into bills(code, code_policy, paid_out) 
values('b6','p3','1');

问题是:如何选择那些已经支付所有保单的人。

我的问题是“路易斯”有两个政策“p1”和“p3”,“p1”已付款,但“p3”未付款。

我的查询:

select ID from living_places where code in (
select code from living_places where code in (
select code_living_place from policies where code_policy in (
select code_policy from bills where paid_out=1 and code_policy not in (
select code_policy from bills where paid_out=0))));

MySQL回复我:

+------+
| ID   |
+------+
| fx1  |
+------+
P.S:“路易斯”没有支付所有保单。例如,账单“b5”未付款。

2 个答案:

答案 0 :(得分:1)

我更喜欢SUM-CASE方法:

SELECT x.name
FROM
(SELECT c.name, SUM(CASE WHEN b.paid_out THEN 0 ELSE 1 END) all_paid
FROM customers c JOIN living_places l ON c.ID = l.ID
JOIN policies p ON l.code = p.code_living_place
JOIN bills b ON p.code_policy = b.code_policy
GROUP BY c.name) x
WHERE x.all_paid = 0;

也许你可以通过在这里使用HAVING子句来避免嵌套的SELECT ...

答案 1 :(得分:0)

我会使用聚合和having子句来解决这个问题:

select p.id
from policies p join
     living_places lp
     on p.code = lp.code_living_place join
     bills b
     on b.code_policy = p.code_policy
group by p.id
having sum(b.paid_out = 1) = count(*);