[英]How to return 0 instead of null when using COUNT in MySQL
I am using this query to return return a list of songs stored in $sTable along with a COUNT of their total projects which are stored in $sTable2.我正在使用此查询返回存储在 $sTable 中的歌曲列表以及存储在 $sTable2 中的项目总数。
/*
* SQL queries
* Get data to display
*/
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns))."
FROM $sTable b
LEFT JOIN (
SELECT COUNT(*) AS projects_count, a.songs_id
FROM $sTable2 a
GROUP BY a.songs_id
) bb ON bb.songs_id = b.songsID
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
'projects_count' is put into an array along with the columns in '$sTable', this is then spat out via JSON and displayed in a table on page. 'projects_count' 与 '$sTable' 中的列一起放入一个数组中,然后通过 JSON 输出并显示在页面上的表格中。
This is working perfectly apart from when a song has no projects linked to it.除了当歌曲没有与之相关的项目时,这是完美的。 It of course returns NULL.它当然返回NULL。
All I want is for any null values to be returned as a '0'.我想要的只是将任何空值作为“0”返回。
I have tried the COUNT(), COUNT(IFNULL (project_id,0) and using COUNT(DISTINCT)...我试过 COUNT(), COUNT(IFNULL (project_id,0) 和使用 COUNT(DISTINCT) ...
And also:-并且:-
SELECT COALESCE(COUNT(*),0) AS projects_count, a.songs_id
All without success.都没有成功。
Any ideas?有任何想法吗?
Use the COALESCE()
function.使用COALESCE()
函数。 COALESCE()
takes at least 2 arguments, calculated in order, and returns the first non-null argument. COALESCE()
至少接受 2 个参数,按顺序计算,并返回第一个非空参数。 So COALESCE(null, 0)
would return 0
, and COALESCE(null, null, null, null, 1)
would return 1
.所以COALESCE(null, 0)
将返回0
,而COALESCE(null, null, null, null, 1)
将返回1
。 Here's MySQL's documentation about COALESCE()
.这是MySQL关于COALESCE()
的文档。
In re-reading your query, you should be able to get the results you want like this:在重新阅读您的查询时,您应该能够得到您想要的结果,如下所示:
SELECT <all the fields you want>, b.songsID, COUNT(*) AS projects_count
FROM $sTable b
LEFT OUTER JOIN $sTable2 bb ON bb.songs_id = b.songsID
$sWhere
GROUP BY b.songsID
$sOrder
$sLimit
Like I said, this should work, but something about it doesn't feel quite right.就像我说的,这应该有效,但感觉不太对劲。
COALESCE()
returns the first non-null argument. COALESCE()
返回第一个非空参数。 So if you say COALESCE(count(...),0)
it will return the count(...)
if it's not null, or it will return 0 if the count(...)
is null因此,如果您说COALESCE(count(...),0)
它将返回count(...)
如果它不为 null,或者如果count(...)
为 null 它将返回 0
You don't have to do the join with a subquery.您不必使用子查询进行连接。 The following should work just fine without the COALESCE etc:如果没有 COALESCE 等,以下应该可以正常工作:
SELECT ".str_replace(" , ", " ", implode(", ", $aColumns)).",
SUM(b.songsID is not null) as countprojects
FROM $sTable b
LEFT JOIN $sTable2 a ON a.songs_id=b.songsID
GROUP BY ".str_replace(" , ", " ", implode(", ", $aColumns))."
This will return what you ask for in countprojects.这将返回您在 countprojects 中要求的内容。
The way it works: The LEFT JOIN
just makes certain you get all data.它的工作方式: LEFT JOIN
只是确保您获得所有数据。 You can't use COUNT
because that would return 1
for the NULL rows.您不能使用COUNT
因为这会为 NULL 行返回1
。 But, if you just use the fact that a boolean TRUE evaluates to 1, and a boolean FALSE evaluates to 0, you can SUM
over those results.但是,如果您仅使用布尔值 TRUE 的计算结果为 1,布尔值 FALSE 的计算结果为 0 的事实,您可以对这些结果SUM
。
Simply add this line in your code after SELECT只需在 SELECT 之后的代码中添加这一行
IF(projects_count IS NULL, 0, projects_count) As projects_countList IF(projects_count IS NULL, 0, projects_count) 作为projects_countList
Like This:像这样:
$sQuery = "
SELECT SQL_CALC_FOUND_ROWS ".str_replace(" , ", " ", implode(", ", $aColumns)).",
IF(projects_countIS NULL, 0, projects_count) As projects_countList
FROM $sTable b
LEFT JOIN (SELECT COUNT(*) AS projects_count, a.songs_id FROM $sTable2 a GROUP BY a.songs_id) bb ON bb.songs_id = b.songsID
$sWhere
$sOrder
$sLimit
";
$rResult = mysql_query( $sQuery, $gaSql['link'] ) or die(mysql_error());
To return 0 instead of null in MySQL在 MySQL 中返回 0 而不是 null
USE用
SELECT id, IF(age IS NULL, 0, age) FROM tblUser SELECT id, IF(age IS NULL, 0, age) FROM tblUser
USE with count() having join 2 tables使用 count() 连接 2 个表
like喜欢
SELECT
tblA.tblA_Id,
tblA.Name,
tblC.SongCount,
IF(tblC.SongCount IS NULL, 0, tblC.SongCount) As noOfSong
FROM tblA
LEFT JOIN
(
SELECT
ArtistId,count(*) AS SongCount
FROM
tblB
GROUP BY
ArtistId
) AS tblC
ON
tblA.tblA_Id = NoOfSong.ArtistId
And Result is结果是
tblA_Id Name SongCount noOfSong
-----------------------------------------------------
7 HSP NULL 0
6 MANI NULL 0
5 MEET 1 1
4 user NULL 0
3 jaani 2 2
2 ammy NULL 0
1 neha 2 2
SELECT blahblahblah, IFNULL(bb.projects_count, 0)
FROM $sTable b
etc...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.