2014-09-20 4 views
0

다음 쿼리를 PHP로 작성된 하위 쿼리와 함께 사용합니다.하나의 하위 쿼리가 null 인 경우 Mysql 쿼리가 null을 반환합니다.

$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_count 또는 user_comment_count에 null을 반환하는 반환 발생합니다.

어떻게해야합니까?

답변

0

MS SQL에 동일한 문제가 있으며 ISNULL() 함수를 사용하여이 문제를 해결합니다. MYSQL에서 IFNULL() 함수를 사용해 보셨습니까?

이 스 니펫을 http://www.w3schools.com/sql/sql_isnull.asp에 발견했습니다. 이처럼 IFNULL() 함수를 사용하여 MySQL의에서

:

SELECT 제품 이름, 단가 * (UnitsInStock + IFNULL (UnitsOnOrder, 0)) 제품의

HTH, 조

관련 문제