dongzhanjuan5141 2011-01-09 06:13
浏览 64
已采纳

MySQL查询失灵?

I have the below MySQL query, which should return the t1.username, t1.website, in_count (WHERE t2.type = 'in'), out_count (WHERE t2.type = 'out') and WHERE the t1.website field is NOT EMPTY or NULL.

However its seemed to have malfunctioned (or I'm doing something wrong?), as it returns 1 result (via mysql_num_rows()) and when i print_r() (on the mysql_fetch_assoc()), the keys of the columns are their however all the values are empty/blank (see below).

The print_r() dump:

Array
(
    [username] => 
    [website] => 
    [in_count] => 
    [out_count] => 
)

The MySQL query:

SELECT t1.username,
       t1.website,
       SUM(IF(t2.type = 'in', 1, 0))  AS in_count,
       SUM(IF(t2.type = 'out', 1, 0)) AS out_count
FROM   users AS t1
       JOIN referrals AS t2
         ON t1.username = t2.author
WHERE  NOT Isnull(t1.website)
LIMIT  0, 10 

Hope all is clear, let me know if you need more information, appreciate all help. :B

  • 写回答

2条回答 默认 最新

  • dongshanni1611 2011-01-09 06:19
    关注

    Sounds like you might need a GROUP BY clause at the end? Perhaps

    GROUP BY t1.username, t1.website
    

    That way you'll get all the in and out counts for a particular user on a particular web site.

    I was under the impression that with aggregate functions like SUM the query requires a GROUP BY clause... I'm surprised the server accepted your SQL.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单片机学习顺序问题!!
  • ¥15 ikuai客户端多拨vpn,重启总是有个别重拨不上
  • ¥20 关于#anlogic#sdram#的问题,如何解决?(关键词-performance)
  • ¥15 相敏解调 matlab
  • ¥15 求lingo代码和思路
  • ¥15 公交车和无人机协同运输
  • ¥15 stm32代码移植没反应
  • ¥15 matlab基于pde算法图像修复,为什么只能对示例图像有效
  • ¥100 连续两帧图像高速减法
  • ¥15 如何绘制动力学系统的相图