gpt4 book ai didi

如果一个子查询为 null,Mysql 查询将返回 null

转载 作者:行者123 更新时间:2023-11-29 23:45:58 25 4
gpt4 key购买 nike

我使用以下查询以及用 PHP 编写的子查询。

$query = mysqli_query($mysqli, "select
users.id as id,
users.display_name as display_name,
users.user_code as user_code,
(select count(articles.id) from articles where articles.user_code = '$user_code') as user_article_count,
(select count(comments.id) from comments where comments.user_code = '$user_code') as user_comment_count
from users");

每当子查询之一与任何行都不匹配时,整个查询就会返回 null,而不是仅为 user_article_countuser_comment_count 返回 null。

如何避免这种情况?

最佳答案

MS SQL 也有同样的问题,我使用 ISNULL() 函数解决了这个问题。你试过 MYSQL 中的 IFNULL() 函数吗?

我在 http://www.w3schools.com/sql/sql_isnull.asp 找到了这个片段.

在 MySQL 中,您可以使用 IFNULL() 函数,如下所示:

选择产品名称,单价*(UnitsInStock+IFNULL(UnitsOnOrder,0)) 来自产品

HTH,乔

关于如果一个子查询为 null,Mysql 查询将返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25949326/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com