Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/307.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/mysql/66.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 运行复杂查询_Java_Mysql_Sql_Database_Relational Database - Fatal编程技术网

Java 运行复杂查询

Java 运行复杂查询,java,mysql,sql,database,relational-database,Java,Mysql,Sql,Database,Relational Database,我的目标是做两件事: -计算特定房间中每个歌曲ID的投票数(此表称为投票数。 -了解用户ID是否对某首歌曲(在特定房间中)至少有一票 userID和RoomID被传递到查询中。我不知道如何“循环”每个songID。我是否运行两个查询—首先获取每个songID,然后运行for循环以获取上述信息(使用Java实现) 如果这不起作用,试试这个-如果用户没有投票支持歌曲,第三列将为空,如果他们投票支持歌曲,UserID select a.SongID, count(*) as voteCount, b

我的目标是做两件事:
-计算特定房间中每个
歌曲ID
投票数(此表称为
投票数

-了解用户ID是否对某首歌曲(在特定房间中)至少有一票

userID
RoomID
被传递到查询中。我不知道如何“循环”每个
songID
。我是否运行两个查询—首先获取每个
songID
,然后运行
for
循环以获取上述信息(使用Java实现)

如果这不起作用,试试这个-如果用户没有投票支持歌曲,第三列将为空,如果他们投票支持歌曲,
UserID

select a.SongID, count(*) as voteCount, b.UserID
from votes a left join votes b on a.songID = b.songID and b.RoomID = ? and b.UserID = ?
where a.RoomID = ?
group by a.SongID, b.UserID
如果这不起作用,试试这个-如果用户没有投票支持歌曲,第三列将为空,如果他们投票支持歌曲,
UserID

select a.SongID, count(*) as voteCount, b.UserID
from votes a left join votes b on a.songID = b.songID and b.RoomID = ? and b.UserID = ?
where a.RoomID = ?
group by a.SongID, b.UserID

在第三个coulmn中试试这个,如果retunr为NULL,则没有投票权

select SongID, count(*) as voteCount,(
        select b.userid from votes b 
        where b.songID = a.songId and b.RoomID = 131 and b.UserID = 70)
    from votes a
    where a.RoomID = 131
    group by SongID

在第三个coulmn中试试这个,如果retunr为NULL,则没有投票权

select SongID, count(*) as voteCount,(
        select b.userid from votes b 
        where b.songID = a.songId and b.RoomID = 131 and b.UserID = 70)
    from votes a
    where a.RoomID = 131
    group by SongID

对于第一个问题,你可以这样做

SELECT Count(songID), songID
FROM votes
WHERE RoomID = @roomid
GROUP BY songID
这是第二个问题

SELECT songID
FROM votes
WHERE UserID = @userid
      AND RoomID = @roomid

对于第一个问题,你可以这样做

SELECT Count(songID), songID
FROM votes
WHERE RoomID = @roomid
GROUP BY songID
这是第二个问题

SELECT songID
FROM votes
WHERE UserID = @userid
      AND RoomID = @roomid

谢谢,看起来我缺少了“分组依据”feautre…知道我如何组合这两个查询吗?谢谢,看起来我缺少了“分组依据”feautre…知道我如何组合这两个查询吗?好的,我查看了MySQL文档,这应该行得通。但我不会为了给你试用而费心安装MySQL。试着去掉“as didUserVote”看看这是否会有不同。好吧,现在我很困惑。你刚刚接受了这个答案;同时发表评论说有错误。它是否工作?我让原始版本工作了一些,但它没有输出“否”…但是空版本对我工作!谢谢,我查看了MySQL文档,应该可以了。但我不会为了给你试用而费心安装MySQL。试着去掉“as didUserVote”看看这是否会有不同。好吧,现在我很困惑。你刚刚接受了这个答案;同时发表评论说有错误。它是否工作?我让原始版本工作了一些,但它没有输出“否”…但是空版本对我工作!谢谢