在一个SQL查询中从另一个表中获取一个正常表和计数(JOIN)

时间:2016-11-09 13:45:47

标签: php mysql sql join

我需要一个SQL查询,它提供一个表的内容和第二个表的计数。我试过了,不能。

我有一个"评论"表。在另一个名为" likings"的表中,有许多评论喜欢。

在我的算法中,在" likings"表格中有一个名为" likeType"的列,如果它是" 1"这意味着喜欢,否则(如果它是" 0")意味着不喜欢。我需要发表评论来自" comments"表,来自" likeType=1"的likeType=0likings的计数一个查询中的表。

这是我最好的尝试,但没有成功:

PHP方面:

   $getFirst8Comments = "SELECT
                            episodecomments.cmtID,
                            episodecomments.cmtConEpisode,
                            episodecomments.cmtOwner,
                            episodecomments.cmtDate,
                            episodecomments.cmtContent,
                            episodecomments.cmtSpoiler,
                            SUM(IF(episodecommentsliking.liType='1', 1, 0)) AS likes,
                            SUM(IF(episodecommentsliking.liType='0', 1, 0)) AS dislikes
                        FROM episodecomments
                        LEFT JOIN episodecommentsliking
                        ON episodecomments.cmtID = episodecommentsliking.liCmtID
                        GROUP BY
                            episodecomments.cmtID,
                            episodecomments.cmtConEpisode,
                            episodecomments.cmtOwner,
                            episodecomments.cmtDate,
                            episodecomments.cmtContent,
                            episodecomments.cmtSpoiler
                        WHERE episodecomments.cmtConEpisode='$epID'
                        ORDER BY episodecomments.cmtID
                        DESC LIMIT 8";

   while ($getF8C = mysqli_fetch_array($getFirst8Comments))
   {
      echo "Something coming through-<br>";
   }
$getFirst8Comments = "SELECT episodecomments.cmtID, episodecomments.cmtConEpisode, episodecomments.cmtOwner, episodecomments.cmtDate, episodecomments.cmtContent, episodecomments.cmtSpoiler, SUM(IF(episodecommentsliking.liType='1', 1, 0)) AS likes, SUM(IF(episodecommentsliking.liType='0', 1, 0)) AS dislikes FROM episodecomments LEFT JOIN episodecommentsliking ON episodecomments.cmtID = episodecommentsliking.liCmtID GROUP BY episodecomments.cmtID, episodecomments.cmtConEpisode, episodecomments.cmtOwner, episodecomments.cmtDate, episodecomments.cmtContent, episodecomments.cmtSpoiler WHERE episodecomments.cmtConEpisode='$epID' ORDER BY episodecomments.cmtID DESC LIMIT 8"; while ($getF8C = mysqli_fetch_array($getFirst8Comments)) { echo "Something coming through-<br>"; }

弹出错误:

提前致谢。

1 个答案:

答案 0 :(得分:1)

MySQL不支持Window。您还需要告知它COUNT对这些聚合(和)函数的内容。

请尝试使用GROUP BY:{SUM如果您计算的是COUNT与非NULL值,则可以正常工作 - 请参阅MySQL documentation)< / p>

NULL

另外 - 如果可能存在没有相似数据的评论记录,请使用“ SELECT comments.cmtID, comments.cmtConEpisode, comments.cmtOwner, comments.cmtDate, comments.cmtContent, comments.cmtSpoiler, SUM(IF(commentsliking.liType='1', 1, 0)) AS likes, SUM(IF(commentsliking.liType='0', 1, 0)) AS dislikes FROM comments INNER JOIN commentsliking ON comments.cmtID = commentsliking.liCmtID GROUP BY comments.cmtID, comments.cmtConEpisode, comments.cmtOwner, comments.cmtDate, comments.cmtContent, comments.cmtSpoiler ORDER BY comments.cmtID DESC LIMIT 8 ”。

我已经进一步更新了我的答案,以解决自LEFT JOIN命令引发的错误。

检查PHP documentation以获取有关如何正确使用mysqli_*函数的完整详细信息 - 特别是关于它需要mysqli_fetch_array()类型作为参数,而不是{{1} [SQL])