Home > Database > Mysql Tutorial > body text

多个leftjoin重复计数

WBOY
Release: 2016-06-07 15:54:27
Original
1346 people have browsed it

各位大神,今天见到一个sql,如下,查询t1.id有在与t2.id进行左联结时的条数,发现运用了大量重复的左联结,感觉很不好,但又不知道怎么修改这个sql,请指教: create table t1(id number);create table t2(id number,val varchar2(10));select a1.id,count(

各位大神,今天见到一个sql,如下,查询t1.id有在与t2.id进行左联结时的条数,发现运用了大量重复的左联结,感觉很不好,但又不知道怎么修改这个sql,请指教:

create table t1(id number);
create table t2(id number,val varchar2(10));

select a1.id,count(1)
  from t1 a1
  left outer join t2 b1
    on a1.id = b1.id
   and b1.val = 'a'
   left outer join t2 b2
   on a1.id=b2.id
   and b2.val='b'
   left outer join t2 b2
   on a1.id=b2.id
   and b2.val='c'
   left outer join t2 b2
   on a1.id=b2.id
   and b2.val='d'
   left outer join t2 b2
   on a1.id=b2.id
   and b2.val='e'
group by a1.id;
Copy after login


Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!