在哪里加入并添加计算列

时间:2018-06-29 10:21:02

标签: mysql

正在寻找帮助。我尝试按JOIN,WHERE和GRP编号的顺序运行代码(在查询结果中)。

我有两个表test和text:

create table test (
    code varchar(10) primary key,
    num int(10) not null,
    name varchar(10) not null,
    surname varchar(10) not null);

insert into test values (1,09,'Tom', 'Smith');
insert into test values (2,09,'Adam','Blake');
insert into test values (3,15,'John','Smith');
insert into test values (4,15,'Adam','XYZ');
insert into test values (5,43,'John','Abc');
insert into test values (6,99,'Adam','Abc');
insert into test values (7,99,'John','Abc');
insert into test values (8,15,'Adam','XYZ');
insert into test values (9,43,'John','Abc');
insert into test values (10,99,'Adam','Abc');
insert into test values (11,99,'John','Abc');
insert into test values (12,1,'A','A');
insert into test values (13,1,'J','A');

和表格文字:

create table text (
    code varchar(10) primary key,
    codeFK varchar(10) not null,
    state varchar(10));

insert into text values (1,2,'O');
insert into text values (2,2,'O');
insert into text values (3,2,'O');
insert into text values (4,2,'C');
insert into text values (5,3,'O');
insert into text values (6,3,'O');
insert into text values (7,3,'O');
insert into text values (8,2,'O');
insert into text values (9,2,'C');
insert into text values (10,2,'O');
insert into text values (11,2,'C');
insert into text values (12,1,'C');
insert into text values (13,12,'C');
insert into text values (14,13,'C');

我加入他们,并尝试按照以下代码添加库伦组:

加入-它有效

SELECT num FROM test
LEFT JOIN text ON test.code = text.codeFK
WHERE state = 'O' 

ORDER BY test.num DESC;

开启GRP计算

select t.*,
       (select count(distinct t2.num)
        from test t2
        where t2.num <= t.num
       ) as grp
from test t ORDER BY GRP;

但是我的问题是如何通过上述联接查询获取GRP列?因此,只会根据基于WHERE语句的结果而不是整个表测试来计算grp列。

我在下面尝试过,但这给了我一个错误:

SELECT num FROM test
LEFT JOIN text ON test.code = text.codeFK
WHERE state = 'O' 

AND test.num IN (select t.num,
       (select count(distinct t2.num)
        from test t2
        where t2.num <= t.num
       ) as grp from test t)

ORDER BY test.num DESC;

所需结果:

enter image description here

2 个答案:

答案 0 :(得分:1)

尝试一下:

select test.num, tt.cnt from test 
join text on test.code = text.codefk
join (
    select t1.num, count(*) cnt from (
        select distinct num
        from test ts1 
    join text tx1 on ts1.code = tx1.codefk
    where tx1.state = 'O'
) t1 join (
    select distinct num
    from test ts2
        join text tx2 on ts2.code = tx2.codefk
        where tx2.state = 'O'
    ) t2 on t1.num <= t2.num
    group by t1.num
) tt on test.num = tt.num
where text.state = 'O'

另一种方式,更有效:

select @lagNum := 0, @cnt := 0;
select num, cnt from (
    select case when num <> @lagNum then @cnt := @cnt + 1 end, @cnt cnt, @lagNum := num, num
    from (
        select (select num from test where code = t.codefk) num
        from text t
        where state = 'O'
    ) a order by num
) a    

答案 1 :(得分:0)

已编辑:这应该为您指明正确的方向。您应该选择

而不是AND中的第二个查询

这是我的小提琴:http://sqlfiddle.com/#!9/2fe48b/36

select t.num,
   (select count(distinct t2.num) - 1
    from test t2
    where t2.num <= t.num
   ) as grp
from test t
LEFT JOIN text ON t.code = text.codeFK
WHERE text.state = 'O' 
ORDER BY t.num DESC;