如何选择特定客户?

时间:2016-05-06 20:31:35

标签: 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)
);

我插入了以下日期:

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');

问题是:如何选择那些已支付所有政策的人?

我的问题是Louis有两个政策p1p3。政策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:Louis没有支付所有政策。例如,帐单b5未付款。

5 个答案:

答案 0 :(得分:2)

我认为应该是这样的:

select name 
from customers
where id not in ( 
    select iving_places.id 
    from living_places
    inner join policies on policies.code_living_place = living_places.code
    inner join bills on ( bills.code_policy = policies.code and bills.paid_out=0)
);

答案 1 :(得分:2)

非常确定这是:

select c.ID, c.name
from customers c
where c.ID in (
    select lp.id
    from policies p
    join living_places lp on p.code_living_place = lp.code
    join bills b on b.code_policy = p.code_policy
    group by lp.id
    having sum(b.paid_out = 1) = count(*));

它会返回0条记录,其中包含您当前拥有的数据,如果您修改了一张路易未付款的账单,那么他将会显示在结果中。

Here's a SQL Fiddle to play around with.

答案 2 :(得分:1)

你是说这个?

select c.ID from customers c
  join living_places l
    on l.ID = c.ID
  join policies p
    on p.code_living_place = l.code
  join bills b
    on b.code_policy = p.code_policy
where paid_out = 0
group by c.ID

答案 3 :(得分:1)

I take it you want customer who have policies paid out i.e paid_out=1?
If yes, this following should do:

select c.ID, c.name
from customers c, living_places l, policies p
where c.ID = l.ID
and l.code = p.code_living_place
and p.code_policy not in (select distinct b.code_policy 
                          from bills b
                          where b.paid_out = '0')

以下是您可以查看结果的SQL Fiddle

编辑:当客户有一个或零个政策(即一个或零个房屋)时,此查询将起作用。 查询需要注意> 1房/政策。

答案 4 :(得分:0)

对不起重复的帖子,答案是:

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;
@David Bachmann Jeppesen的回复:Select a specific customer in MYSQL?