简体   繁体   中英

SQL - alternative to left outer join

There is a standard way in SQL to conut a number of rows joined to one table acepting also the 0? That is one example :

SELECT t1.id, COUNT(t2.*)
FROM t1 LEFT OUTER JOIN t2 ON ( t1.id = t2.id )
GROUP BY t1.id

I need a alternative because i use odbc with different databases, and on some databases the left join aren't supported.

SELECT 
  t1.id, 
  (SELECT COUNT(*) FROM t2 WHERE t2.id = t1.id) as t2_count 
FROM t1

Two Options: Option 1: use the (+) operator:

SELECT t1.id, COUNT(t2.*)
  FROM t1, t2
 WHERE t2.id(+) = t1.id
 GROUP BY t1.id

I don;t know if it works on all drivers. Option 2 that will work with all drivers is to create a view and create the view instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM