1

问题出在我的查询上。我需要从两个表中选择数据,table1 有一个字段类别,即类别 id,表 2 有字段 id,即类别 id。我需要按月选择数据,例如,如果我们选择 2 月,我将在选定期间计算该表中的所有内容并按类别 ID 分组。例如,在表 1 中选定的月份中,没有类别 id=3,十个从表 2 中取出它并使该类别值=0。如果在 table1 的 3 月期间是 id=3 的类别,则计算具有该类别 id 的所有值。

我做了一个选择,但它不正确。

$query = sprintf("SELECT count(*) as counter, category, (select category from categories where id=leads.category) as categoryname, clientcode , language                   
                  FROM leads   
                  WHERE synced > %s and synced < %s
                  GROUP BY category, clientcode 
               UNION ALL
                  SELECT 0 as counter, id as category, category as categoryname, 0 as clientcode, language  FROM categories WHERE id NOT IN(SELECT category FROM leads)
                  GROUP BY category, clientcode ",$starttime, $endtime ); 

感谢您的帮助,希望您了解我的需要。

很多文字...

我解决了这个问题:

$query = sprintf("SELECT count(*) as counter, category, (select category from categories where id=leads.category) as categoryname, clientcode , language                   
                      FROM leads   
                      WHERE synced > %s and synced < %s
                      GROUP BY category, clientcode 
                   UNION ALL
                      SELECT 0 as counter, id as category, category as categoryname, 0 as clientcode, language  FROM categories WHERE id NOT IN(SELECT category FROM leads WHERE synced > %s and synced < %s)
                      GROUP BY category, clientcode ",$starttime, $endtime, $starttime, $endtime ); 
4

0 回答 0