表1(用户的评论):comment_id,user_id,comment
表2(用户):user_id,user_name
表3(对某评论的投票):comment_id,user_id,vote
我主要是想查询出这样的数据(表1.comment,表2.user_name,SUM(表3.vote))
我的sql语句是这样的
SELECT 表1.comment_id,表1.user_id,comment,user_name,SUM(vote)
FROM (表1 INNER JOIN 表2 ON (表1.user_id = 表2.user_id))
      LEFT JOIN 表3 ON (表1.comment_id = 表3.comment_id)
WHERE 表1.user_id REGEXP :user_id AND 表1.music_id REGEXP :music_id但是失败了,请问应该怎样写呢?另外如果去掉LEFT JOIN的部分则剩下的没问题,
就是可以获得正确的(表1.comment,表2.user_name)SQLmysql连接查询

解决方案 »

  1.   

    SELECT 表1.comment_id,表1.user_id,表1.comment,表2.user_name,SUM(vote)
    FROM 表1 
    INNER JOIN 表2 ON 表1.user_id = 表2.user_id
    LEFT JOIN 表3 ON 表1.comment_id = 表3.comment_id
    where ....
    group by 表1.comment_id,表1.user_id,表1.comment,表2.user_name
      

  2.   

    select  a.comment,b.user_name,SUM(c.vote) as sum_vote from 表1 a,表2 b,表3 c
    where a.user_id=b.user_id  and b.user_id=c.user_id
    group by a.comment,b.user_name
      

  3.   

       建议你列出你的表结构,并提供测试数据以及基于这些测试数据的所对应正确结果。
       参考一下这个贴子的提问方式http://topic.csdn.net/u/20091130/20/8343ee6a-417c-4c2d-9415-fa46604a00cf.html
       
       1. 你的 create table xxx .. 语句
       2. 你的 insert into xxx ... 语句
       3. 结果是什么样,(并给以简单的算法描述)
       4. 你用的数据库名称和版本(经常有人在MS SQL server版问 MySQL)
       
       这样想帮你的人可以直接搭建和你相同的环境,并在给出方案前进行测试,避免文字描述理解上的误差。