来自2个表的SQLite比较查询

来自2个表的SQLite比较查询,sqlite,Sqlite,我想创建一个新的查询,以了解商店中有多少产品 表:tb_商店 +--------+------------+------------------+-----------+ | item_id| nome | data_out | data_in | +--------+------------+------------------+-----------+ | 1 | Produ1 | null

我想创建一个新的查询,以了解商店中有多少产品

表:tb_商店

    +--------+------------+------------------+-----------+
    | item_id| nome       |  data_out        |   data_in |
    +--------+------------+------------------+-----------+
    | 1      | Produ1     | null             | 2015-01-06|
    | 2      | Produ1     | null             | 2015-01-06|
    | 3      | Produ3     | null             | 2015-01-06|
    | 4      | Produ3     | null             | 2015-01-06|
    | 5      | Produ5     | null             | 2015-01-06|
    | 6      | Produ4     | 2015-01-06       | 2015-02-06|
    | 7      | Produ2     | 2015-01-06       | 2015-02-06|
    +--------+------------+------------------+-----------+
表:tb\U产品

+--------+------------+
| item_id| nome       |
+--------+------------+
| 1      | Produ1     |
| 2      | Produ2     |
| 3      | Produ3     |
| 4      | Produ4     |
| 5      | Produ5     |
+--------+------------+
我写了这个问题:

select nome, count(nome) as pezzi from  tb_store where data_out is null or data_out="" group by nome order by pezzi desc
结果是:

+--------+------------+
| nome   | pezzi      |
+--------+------------+
| Produ1 | 2          |
| Produ3 | 2          |
| Produ5 | 1          |
+--------+------------+
我想得到这个结果:

+--------+------------+
| nome   | pezzi      |
+--------+------------+
| Produ1 | 2          |
| Produ3 | 2          |
| Produ5 | 1          |
| Produ2 | 0          |
| Produ4 | 0          |
+--------+------------+
有可能吗?如何重写查询

编辑

我创建了一个新查询,如下所示:

    SELECT DISTINCT nome, pezzi FROM(
SELECT nome,COALESCE(pezzi,0)as pezzi FROM(
SELECT p.nome, COUNT(s.nome) as pezzi
FROM tb_product as p LEFT JOIN tb_store as s
ON   p.nome = s.nome
WHERE s.data_out is null
OR    s.data_out = ""
GROUP BY p.nome
union
select nome,null as pezzi from tb_product) )
ORDER BY pezzi DESC
但是我有一些重复的项目要删除…结果是

nome         pezzi
crema         2
pistacchio    2
zabajone      2
bacio         1
cassata       1
cioccolato    1
ciocco rum    1
malaga        1
mango         1
mascarpone    1
nocciola      1
stracciatella 1
bacio         0
caramello     0
cassata       0
cioccolato    0
ciocco rum    0
crema         0
fragola       0
limone        0
malaga        0
mango         0
mascarpone    0
nocciola      0
pistacchio    0
zabajone      0
是否可以删除0的副本

SELECT DISTINCT nome, pezzi FROM(
SELECT nome,COALESCE(pezzi,0)as pezzi FROM(
SELECT p.nome, COUNT(s.nome) as pezzi
FROM tb_product as p LEFT JOIN tb_store as s
ON   p.nome = s.nome
WHERE s.data_out is null
OR    s.data_out = ""
GROUP BY p.nome
union
select nome,null as pezzi from tb_product) GROUP BY nome ) 
ORDER BY pezzi DESC

您需要在两个表之间执行左联接,如:

SELECT p.nome, COUNT(s.nome) as pezzi
FROM tb_product as p LEFT JOIN tb_store as s
ON   p.name = s.name
WHERE data_out is null
OR    data_out = ""
GROUP BY p.nome
ORDER BY pezzi DESC

我已经尝试在Sqlite的DB Browser上模拟查询,结果是:没有这样的列:date\u out:选择p.nome,将(s.nome)计数为来自tb\u产品p的pezzi LEFT JOIN tb\u store s on p.name=s.name,其中date\u out为null或date\u out=“”按p.nome分组按pezzi Desce排序以上在您的帖子中,您向我们展示了您的日期字段,现在您发现了错误?请检查确切的名称好吗?好的,查询运行,但只显示数据>0,不显示数据=0。我已经发布了文件(mycpstore.db)。例如:更新表,但我认为问题在于where条件,可能我需要在两个不同的查询中创建apped od数据,因为where表示数据为null或空。那你现在在看什么?