0

所以我确定我特别密集 - 但我正在努力解决 PHP 中应该是一个简单的(mysql)SQL 语句。

我有两张桌子:

  • entry包含参赛作品的名称和详细信息。
  • votes包含时间戳、唯一 ID 和 entry_id。entry_id 与条目 id 匹配的每一行代表对该人的竞赛条目的一票。

粗略的数据库结构是:

入口表:

|id|name|email|created|deleted|   [last two cols being datetime stamps]

投票表:

|id|entry_id|created|deleted|

我的尝试(各种重写,可能太累了!)导致只返回一行。

我最近的尝试:

$sql = 'SELECT
        s_id,
        count(sv.s_id) as count
    FROM
        'vote AS sv
    LEFT JOIN
        'entry AS se on sv.entry_id = se.id
    WHERE
        sv.deleted = "0000-00-00 00:00:00"
    AND
        se.deleted = "0000-00-00 00:00:00"
    ORDER BY
        count DESC
    LIMIT
        10';

有人可以指导我如何最好地实现这一目标吗?

我的目标是返回的前十个条目(按投票计数),计数包含在返回数据中。

谢谢!

史蒂夫

4

2 回答 2

2

怎么样

SELECT e.id, COUNT(v.id) AS numVotes
FROM entry AS e
JOIN vote AS v ON v.entry_id = e.id
WHERE e.deleted = "0000-00-00 00:00:00"
AND v.deleted = "0000-00-00 00:00:00"
GROUP BY e.id
ORDER BY numVotes DESC
LIMIT 10

?

于 2013-09-29T21:06:15.933 回答
0
select
        e.*,
        cnt
from
        entry e,
(
        select
                entry_id,   
                count(*) cnt
        from
                votes
        where
                deleted = "0000-00-00 00:00:00"
        group by
                entry_id
        order by
                count(*) desc
        limit 10
) c  
where
        c.entry_id = e.id and
        e.deleted = "0000-00-00 00:00:00"
order by
        cnt desc
于 2013-09-29T21:10:44.473 回答