我有两张这样的表
表1
orig1 orig2 orig3 xref1 xref2 xref3
1 1 1 2 2 2
1 1 1 3 3 3
23 23 23 12 12 12
表2
orig1 orig2 orig3 xref1 xref2 xref3 version
1 1 1 1 1 1 0
expected output:-
orig1,orig2,orig3,count_table2
1,1,1,1
我正在尝试选择table1中的前3列和table2中的count(*)。我试过这个
选择 来自table1的orig1,orig2,orig3, COUNT(table2。*)为t2, FROM table1 LEFT JOIN tabel2 ON table1.orig1 = table2.orig1
仅打印NUll。任何帮助将不胜感激。
答案 0 :(得分:2)
我不能说你的预期输出对我有意义,但你可以得到你想要的结果
drop table if exists table1, table2;
create table table1(orig1 int,orig2 int,orig3 int,xref1 int,xref2 int,xref3 int);
insert into table1 values
(1 , 1 , 1 , 2 , 2 , 2),
(1 , 1 , 1 , 3 , 3 , 3),
(23 , 23 , 23 , 12 , 12 , 12);
create table table2(orig1 int, orig2 int, orig3 int,xref1 int,xref2 int,xref3 int, version int);
insert into table2 values
(1 , 1 , 1 , 1 , 1 , 1 , 0);
select distinct t1.orig1,t1.orig2,t1.orig3,obs
from table1 t1
join
(select t2.orig1,count(*) obs
from table2 t2
group by t2.orig1) t2
on t2.orig1 = t1.orig1;
+-------+-------+-------+-----+
| orig1 | orig2 | orig3 | obs |
+-------+-------+-------+-----+
| 1 | 1 | 1 | 1 |
+-------+-------+-------+-----+
1 row in set (0.00 sec)