简体   繁体   中英

Select distincts of same column in mutiple tables sql

SQL novice here.

I have this schema:

其中节点表和方式表具有相同的列uid

What I need in plain English is:

"Out of the 2 columns, make one above the other in one column, and then count how many distincts values there is"

I've tried

SELECT COUNT (DISTINCT uid ) from nodes  UNION SELECT COUNT (DISTINCT uid) from ways ;

SELECT   distinct nodes.uid  from nodes  JOIN ways on nodes.uid = ways.uid ;

sqlite> SELECT COUNT (DISTINCT uid ) from nodes  UNION SELECT COUNT (DISTINCT uid) from ways ;
1195
2182
sqlite> SELECT   uid  from nodes  FULL OUTER JOIN ways on nodes.uid = ways.iud ;
Error: RIGHT and FULL OUTER JOINs are not currently supported

SELECT COUNT (DISTINCT iud) FROM (SELECT DISTINCT uid from nodes as uid UNION SELECT DISTINCT uid from ways as uid as subq);

SELECT   count (distinct nodes.uid)  from nodes  JOIN ways on nodes.uid = ways.uid ;

takes ages and i'm not sure nodes.uid = ways.uid is the correct way to go

Any idea ?

我想我明白了

SELECT COUNT (DISTINCT uid) from (SELECT DISTINCT uid  from nodes  UNION SELECT  DISTINCT uid from ways) as subq ;

Since UNION returns a distinct set of the joining tables you dont need to use the DISTINCT keyword

SELECT COUNT(uid) Cnt
FROM   (
    SELECT uid 
    FROM   nodes
    UNION 
    SELECT uid
    FROM   ways
) t

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