Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/sqlite/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
sqlite在计数中包含0_Sqlite_Left Join - Fatal编程技术网

sqlite在计数中包含0

sqlite在计数中包含0,sqlite,left-join,Sqlite,Left Join,我在一个SQLite数据库中有两个表,我试图通过评级来计算路由数。它可以工作,但当没有具有该等级的路由时,不会返回0。例如,评级为8、11b或V3 我现在使用的查询是: select routes.rating, count(routes.rating) from routes left join orderkeys on routes.rating = orderkeys.rating group by routes.rating order by orderkeys.key 但是,对于没

我在一个SQLite数据库中有两个表,我试图通过评级来计算路由数。它可以工作,但当没有具有该等级的路由时,不会返回0。例如,评级为8、11b或V3

我现在使用的查询是:

select routes.rating, count(routes.rating) from routes 
left join orderkeys on routes.rating = orderkeys.rating
group by routes.rating
order by orderkeys.key
但是,对于没有任何路由的评级,这不会返回0。我得到的结果是:

10d|3
7|3
8|2
9|9
10a|5
10b|4
10c|2
11a|3
12b|1
V0|5
V1|7
V2|5
V3|8
V4|3
V5|1
V6|2
V7|3
V8|2
V9|1
我希望得到的是:

7|3
8|2
9|9
10a|5
10b|4
10c|2
10d|3
11a|3
11b|0
11c|0
11d|0
12a|0
12b|1
12c|0
12d|0
V0|5
V1|7
V2|5
V3|8
V4|3
V5|1
V6|2
V7|3
V8|2
V9|1
以下是模式:

CREATE TABLE routes (
    id integer PRIMARY KEY,
    type text, rating text, 
    rope integer, 
    name text, 
    date text, 
    setter text, 
    color_1 text, 
    color_2 text, 
    special_base text, 
    blurb text, 
    updater text
);
CREATE TABLE orderkeys (
    rating TEXT,
    key INTEGER PRIMARY KEY
);

试试这个。我不太喜欢join,但当有很多表时,它非常有用:

select routes.rating, count(routes.rating) from routes, rating 
where routes.rating = orderkeys.rating
group by routes.rating
order by orderkeys.key

左联接返回左表中的所有记录,但您需要的是所有评级,即
orderkeys
表中的所有记录:

SELECT orderkeys.rating,
       COUNT(routes.id)
FROM orderkeys
LEFT JOIN routes USING (rating)
GROUP BY orderkeys.rating
ORDER BY orderkeys.key

对于没有相应路由的评级,它仍然不会返回0。这与内部联接完全相同,内部联接返回的记录数不能超过问题中的查询数。