2012-01-04 63 views
2

我有这个疑问:SQL查询觉得没有什么,但返回1行的所有空

select problems.problem_id , creator_member_id , problem_title , problem_description , sum(vote) as totalVotes , DAYOFMONTH(problem_date) , DAYNAME(problem_date) , YEAR(problem_date) , MONTH(problem_date) , first_name , last_name , email , small_thumb , mid_thumb 
    from problems 
     left join problem_votes 
      on problems.problem_id = problem_votes.problem_id 
     left join users 
      on problems.creator_member_id = users.user_id 
     left join member_photo 
      on problems.creator_member_id = member_photo.member_id 
    where problems.problem_id = 1; 

它没有匹配项,并返回此:

-+--------------------+---------------------+------------+-----------+-------+-------------+-----------+ 
| problem_id | creator_member_id | problem_title | problem_description | totalVotes | DAYOFMONTH(problem_date) | DAYNAME(problem_date) | YEAR(problem_date) | MONTH(problem_date) | first_name | last_name | email | small_thumb | mid_thumb | 
+------------+-------------------+---------------+---------------------+------------+--------------------------+-----------------------+--------------------+---------------------+------------+-----------+-------+-------------+-----------+ 
|  NULL |    NULL | NULL   | NULL    |  NULL |      NULL | NULL     |    NULL |    NULL | NULL  | NULL  | NULL | NULL  | NULL  | 
+------------+-------------------+---------------+---------------------+------------+--------------------------+-----------------------+--------------------+---------------------+------------+-----------+-------+-------------+-----------+ 

但我不知道为什么它在返回任何东西所有?模式有什么问题吗?或者查询?

我正在检查它是否返回任何内容,并试图返回404页面,但系统认为有1个返回的行,所以它会混淆我的应用程序。

任何我可能在这里做错了?

谢谢!

+4

无法看到它是如何从任何回报你明确强制它只返回其中problems.problem_id = 1的行。 – 2012-01-04 16:36:31

+0

@MarcB我也是 - 非常奇怪,对不对? :) – GeekedOut 2012-01-04 16:44:00

+0

我相信这与使用'sum'这样的聚合函数没有'group by'子句有关。添加'group by'子句可能会修复它,或者您可能还需要将条件更改为'having'而不是'where'。 (或者我可能是完全错误的。) – grossvogel 2012-01-04 16:45:05

回答

3

尝试加入以下条款到您的查询的末尾:

GROUP BY problems.problem_id, creator_member_id, problem_title, 
problem_description, problem_date, first_name, last_name, 
email, small_thumb ,mid_thumb 

最佳实践是被选中,但没有聚集(如投票)必须每列在group by子句中,尽管MYSQL不强制执行此操作。相反,它表现得很奇怪,让人感到困惑。 (对于一个更好的解释见this answer

+0

这是我的问题。来自PostgreSQL的背景,我习惯了数据库不让我sl ...不驯...... – Nostradamnit 2012-12-06 13:06:11

-1

首先,确保你没有行与所有NULL

则:LEFT JOIN关键字返回左表(table_name1)的所有行,即使在右表(不匹配table_name2)。

尝试将其更改为INNER JOIN

+1

WHERE子句过滤'problems.problem_id',它是连接中最左边表中的一个值。然而结果显示'problems.problem_id'为NULL。更改为INNER JOIN在这里不起作用。我怀疑这个问题不在SQL本身之内。 – MatBailie 2012-01-04 16:37:28

3

的问题是由于

sum(vote) as totalVotes 

如果你想使用聚合函数SUM分组结果集,你必须使用一个 语句GROUP通过。

如果你希望筛选的结果,你应该使用具有

http://database-programmer.blogspot.com/2008/04/group-by-having-sum-avg-and-count.html

你的情况,我认为这应该工作:

SELECT problems.problem_id , creator_member_id , problem_title , problem_description , SUM(vote) AS totalVotes , 
DAYOFMONTH(problem_date) , DAYNAME(problem_date) , YEAR(problem_date) , MONTH(problem_date) , first_name , last_name , email , small_thumb , mid_thumb 
FROM problems 
    LEFT JOIN problem_votes 
     ON problems.problem_id = problem_votes.problem_id 
    LEFT JOIN users 
     ON problems.creator_member_id = users.user_id 
    LEFT JOIN member_photo 
     ON problems.creator_member_id = member_photo.member_id 
WHERE problems.problem_id = 1; 
GROUP BY problems.problem_id , creator_member_id , problem_title , problem_description , problem_date , first_name , last_name , email , small_thumb , mid_thumb