Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/sql/75.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
Sql 用count(*)子句选择_Sql_Oracle - Fatal编程技术网

Sql 用count(*)子句选择

Sql 用count(*)子句选择,sql,oracle,Sql,Oracle,我的数据库表: //TABLE1 tname ----------- brazil spain england 在我的表2中: //Table2 title date ------------------------- test1 15-jun-2012 test2 16-jun-2012 test3 14-AUG-2011 在我的表3中: //Table3 Tname title ----

我的数据库表:

//TABLE1
tname       
-----------
brazil     
spain       
england    
在我的表2中:

//Table2
title      date
-------------------------
 test1      15-jun-2012
 test2      16-jun-2012
 test3      14-AUG-2011
在我的表3中:

//Table3
Tname      title
------------------
 brazil     test1
 brazil     test2
 spain      test3
我的问题是找到自2012年以来发生的tname,即2012年1月1日

所以我的问题是

 select tname,
        NVL(count(*),0) as total 
   from table1 t1 
  inner join table3 t3 
     on t1.tname = t3.tname 
  inner join table2 t2 
     on t3.title = t2.title
  where (TO_CHAR(date,'yyyy-mm-dd') > '2012-01-01');
预期产出:

//result
tname        total
-------------------
 brazil        2
 spain         0
 england       0

您需要一个
分组依据
子句:

 select tname, NVL(count(*),0) as total
 from table1 t1 inner join
      table3 t3 
      on t1.tname = t3.tname inner join
      table2 t2
      on t3.title = t2.title
 where (TO_CHAR(date,'yyyy-mm-dd') > '2012-01-01')
 group by tname;

这假设查询的其余部分是正确的。

日期的字符串比较通常不会得到您真正想要的结果。 并且需要GROUPBY,或者应该返回oracle的语法错误

SELECT tname,NVL(count(*),0) as total 
FROM  table1 t1 
INNER JOIN table3 t3 
   on t1.tname = t3.tname 
inner join table2 t2 
   on t3.title = t2.title
where date > to_date('2012-01-01','yyyy-mm-dd')
GROUP BY tname;

日期是什么数据类型?字符串compairison不是你真正想要的东西,是吗?我无法测试查询。不知道为什么。但这是正确的吗?@user3664490。虽然
nvl()
是多余的,但在我看来它是正确的<代码>计数(*)不返回
NULL