2014-02-13 90 views
0

以这种方式查询我的结果数据。根据列选择不同的行

Id Size 
123  1 
123  1 
123  2 
123  2 
134  1 
134  1 
134  2 

我想要的结果让我计数消除重复的大小像

Id Size 
123  1 
123  2 
134  1 
134  2 

上方是连接两个表的结果。问题是我不能在这种情况下使用不同的。

这里是表如何

Table1: 
    Id Created ... .. .. .. 
    123 date1 .... 
    134 date2 .... 

Table2:  
Id Size 
123  1 
123  2 
134  1 
134  2 

我有我的查询,从表1基于CreatedDate选择,它这样

select count(*) 
from table1 

join table2 
on table1.id = table2.id 

where table1.creates between '' and ''. 

你如何得到不同尺寸。

如果我使用select count(distinct table2.size),它只对所有行返回1和2。

回答

0
SELECT DISTINCT Id, Size 
    FROM table1 

这应该给你一个不同的Id和大小组合的列表。

+0

是的它应该工作。更准确地说, SELECT DISTINCT ID,Size FROM table1,table2 WHERE table1.Id = table2.Id AND'table1.creates'和'' – SNathan

0
select count(distinct table1.id, table2.size) 
from table1 

join table2 
on table1.id = table2.id 

where table1.creates between '' and '' 

有时解决方案是那么明显...... :)

更新:另一种方式

select count(*) from (
    select distinct table1.id, table2.size 
    from table1 

    join table2 
    on table1.id = table2.id 

    where table1.creates between '' and '' 
) sq 
+0

count会导致它只显示count,而不是行本身。 – Codeman

+0

感谢您的回复。如上所述,我没有看到count()的工作。 ','附近的语法不正确。我应该使用不同的语法吗? – user3285499

+0

@ Pheonixblade9嗯,在问题中,它被问及数量,而不是行。 – fancyPants