关于select count()

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了关于select count()相关的知识,希望对你有一定的参考价值。

1、首先想知道select count(*) from 的from后面是不是只可以跟一个表名?

2、下面的语句是从两个表中检索的SQL文,请问如何取得其检索结果的条数,也就是用select count(*)该怎么写?望高手指点!

SELECT
STM_MITSUMORI_JOKEN_PATTERN.MITSUMORI_JOKEN_PAT_NO
,STM_MITSUMORI_JOKEN_PATTERN.MITSUMORI_JOKEN_PAT_NM
,STM_MITSUMORI_JOKEN_PATTERN.BIKO
,STM_TORIHIKISAKI.TORIHIKISAKI_CODE
,STM_TORIHIKISAKI.TORIHIKISAKI_NM

FROM
STM_MITSUMORI_JOKEN_PATTERN A
,STM_TORIHIKISAKI B

WHERE
STM_MITSUMORI_JOKEN_PATTERN.RIYO_KIGYO_CODE=#riyo_Kigyo_Code#
<isNotEmpty property="mitsumori_Joken_Pat_No_From">
AND A.MITSUMORI_JOKEN_PAT_NO >= #mitsumori_Joken_Pat_No_From#
</isNotEmpty>
<isNotEmpty property="mitsumori_Joken_Pat_No_To">
AND A.MITSUMORI_JOKEN_PAT_NO <= #mitsumori_Joken_Pat_No_To#
</isNotEmpty>
<isNotEmpty property="mitsumori_Joken_Pat_Nm">
AND A.MITSUMORI_JOKEN_PAT_NM like #mitsumori_Joken_Pat_Nm# || '%'
</isNotEmpty>
<isNotEmpty property="tokuisaki_Code">
AND A.TOKUISAKI_CODE=#tokuisaki_Code#
</isNotEmpty>
AND B.RIYO_KIGYO_CODE (+)= A.RIYO_KIGYO_CODE
AND B.TORIHIKISAKI_CODE (+)= A.TOKUISAKI_CODE

<dynamic prepend="order by">
<isEqual prepend="," property="narabekae1" compareValue="1">
B.HANSHA_KBN
</isEqual>
<isEqual prepend="," property="narabekae1" compareValue="2">
B.HANSHA_KBN DESC
</isEqual>
<isEqual prepend="," property="narabekae1" compareValue="3">
A.MITSUMORI_JOKEN_PAT_NO
</isEqual>
<isEqual prepend="," property="narabekae1" compareValue="4">
A.MITSUMORI_JOKEN_PAT_NO DESC
</isEqual>
<isEqual prepend="," property="narabekae1" compareValue="5">
A.TOKUISAKI_CODE
</isEqual>
<isEqual prepend="," property="narabekae1" compareValue="6">
A.TOKUISAKI_CODE desc
</isEqual>
,<isEqual prepend="," property="narabekae2" compareValue="1">
B.HANSHA_KBN
</isEqual>
<isEqual prepend="," property="narabekae2" compareValue="2">
B.HANSHA_KBN DESC
</isEqual>
<isEqual prepend="," property="narabekae2" compareValue="3">
A.MITSUMORI_JOKEN_PAT_NO
</isEqual>
<isEqual prepend="," property="narabekae2" compareValue="4">
A.MITSUMORI_JOKEN_PAT_NO DESC
</isEqual>
<isEqual prepend="," property="narabekae2" compareValue="5">
A.TOKUISAKI_CODE
</isEqual>
<isEqual prepend="," property="narabekae2" compareValue="6">
A.TOKUISAKI_CODE desc
</isEqual>
</dynamic>

多谢了!!!

可以跟多个表名,但要用join连接起来,如
select count(*) from ta full join tb on ta.id=tb.id
参考技术A select count(*) from 的from后面可以只跟一个表名
它统计的是结果集中至少包含一个非NULL值的行数
就是统计不全是NULL值的行数

Select Count (*)和Select Count

一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的

    假如表沒有主键(Primary key), 那么count(1)比count(*)快,

    如果有主键的話,那主键作为count的条件时候count(主键)最快

    如果你的表只有一个字段的话那count(*)就是最快的

   count(*) 跟 count(1) 的结果一样,都包括对NULL的统计,而count(column) 是不包括NULL的统计

以上是关于关于select count()的主要内容,如果未能解决你的问题,请参考以下文章

关于Mysql Select Count(*或者主键) 查询速度缓慢的解决办法

关于oracle存储过程select into 未找到数据问题

关于count(id)查询慢的问题,请高手帮助解答

关于mysql统计的问题。

关于mysql数据库中重复数据的操作

关于数据库优化1——关于count,count(*),和count(列名)的区别,和关于表中字段顺序的问题