2

我目前正在从我的数据库中选择“想法”,但另一个要求是能够抓住“趋势想法”,即过去 7 天内投票最多的 10 个想法。

我选择“想法”的查询是这样的:

  SELECT
  t.id AS 'id',
  CONCAT(t.first_name, ' ', SUBSTRING(t.last_name,1,1)) AS 'name',
  t.votes_up,
  t.votes_down,
  t.votes_aggregate,
  GROUP_CONCAT(DISTINCT tags.name) AS 'tags',
  t.createdon AS 'timestamp'
  FROM (
    SELECT
      ideas.id, first_name, last_name, createdon,
      COALESCE(SUM(case when value > 0 then value end),0) votes_up,
      COALESCE(SUM(case when value < 0 then value end),0) votes_down,
      COALESCE(SUM(value),0) votes_aggregate
      FROM ideas
      LEFT JOIN votes ON ideas.id = votes.idea_id
      GROUP BY ideas.id
  ) as t
  LEFT JOIN tags_rel ON t.id = tags_rel.idea_id
  LEFT JOIN tags ON tags_rel.tag_id = tags.id

votes_up我将如何获得并显示所有选票,但只能获得在过去 7 天内被投票( )并按数量排序的“想法” votes_up

这是我的尝试:

  SELECT
      t.id AS 'id',
      CONCAT(t.first_name, ' ', SUBSTRING(t.last_name,1,1)) AS 'name',
      t.votes_up,
      t.votes_down,
      t.votes_aggregate,
      GROUP_CONCAT(DISTINCT tags.name) AS 'tags',
      t.createdon AS 'timestamp'
      FROM (
        SELECT
          ideas.id, first_name, last_name, createdon,
          COALESCE(SUM(case when value > 0 then value end),0) votes_up,
          COALESCE(SUM(case when value < 0 then value end),0) votes_down,
          COALESCE(SUM(value),0) votes_aggregate
          FROM ideas
          LEFT JOIN votes ON ideas.id = votes.idea_id
          GROUP BY ideas.id
      ) as t
      LEFT JOIN tags_rel ON t.id = tags_rel.idea_id
      LEFT JOIN tags ON tags_rel.tag_id = tags.id
  WHERE t.published = 1
  AND (
    SELECT ideas.id,
    COALESCE(SUM(case when value > 0 then value end),0) votes_up
    FROM ideas
    LEFT JOIN votes ON ideas.id = votes.idea_id
    WHERE votes.`timestamp` > (NOW() - INTERVAL 7 DAY)
    GROUP BY ideas.id
  ) as v
  GROUP BY t.id
  ORDER BY v.votes_up DESC

但我得到了错误for the right syntax to use near 'as v GROUP BY t.id ORDER BY v.votes_up DESC LIMIT 10'

4

2 回答 2

1

您在 WHERE 子句中使用了“AS v”,这是不可能的:

WHERE t.published = 1
  AND (
    SELECT ideas.id,
    COALESCE(SUM(case when value > 0 then value end),0) votes_up
    FROM ideas
    LEFT JOIN votes ON ideas.id = votes.idea_id
    WHERE votes.`timestamp` > (NOW() - INTERVAL 7 DAY)
    GROUP BY ideas.id
  ) as v
于 2013-09-13T10:16:38.980 回答
0

试试这个,在您的 SELECT 语句中执行子查询,在您的 WHERE 中检查这是否不是 NULL 并且您可以按它排序(如果我是对的):

SELECT
      t.id AS 'id',
      CONCAT(t.first_name, ' ', SUBSTRING(t.last_name,1,1)) AS 'name',
      t.votes_up,
      t.votes_down,
      t.votes_aggregate,
      GROUP_CONCAT(DISTINCT tags.name) AS 'tags',
      t.createdon AS 'timestamp',
      (
         SELECT ideas.id,
         COALESCE(SUM(case when value > 0 then value end),0) votes_up
         FROM ideas
         LEFT JOIN votes ON ideas.id = votes.idea_id
         WHERE votes.`timestamp` > (NOW() - INTERVAL 7 DAY)
         GROUP BY ideas.id
      ) AS vup 
      FROM (
        SELECT
          ideas.id, first_name, last_name, createdon,
          COALESCE(SUM(case when value > 0 then value end),0) votes_up,
          COALESCE(SUM(case when value < 0 then value end),0) votes_down,
          COALESCE(SUM(value),0) votes_aggregate
          FROM ideas
          LEFT JOIN votes ON ideas.id = votes.idea_id
          GROUP BY ideas.id
      ) as t
      LEFT JOIN tags_rel ON t.id = tags_rel.idea_id
      LEFT JOIN tags ON tags_rel.tag_id = tags.id
  WHERE t.published = 1
  AND vup IS NOT NULL
  GROUP BY t.id
  ORDER BY vup DESC
于 2013-09-13T10:57:15.397 回答