假设有以下三个表:
td_idea
+----------+--------------+----------+ ------------------+ | 想法ID | 想法名称 | idea_submitter_id | 想法状态 | +----------+--------------+----------+ ------------------+ | 1 | 衬衫 | 3 | 设计 | | 2 | 顶部 | 1 | 颜色 | +----------+--------------+----------+ ------------------+
td_idea_contribution
+-----------------+------------+----------------- + | 贡献ID | 想法ID | 提交者 ID | +-----------------+------------+----------------- + | 1 | 1 | 3 | | 2 | 1 | 4 | | 3 | 1 | 7 | | 4 | 1 | 10 | | 5 | 2 | 3 | | 6 | 2 | 10 | +-----------------+------------+----------------- +
此表显示了用户对特定想法所做的贡献。
td_idea_contribution_like
+----------+--------------------------------+----------------- + | like_id | 贡献ID | 提交者 ID | +----------+--------------------------------+----------------- + | 1 | 1 | 1 | | 2 | 1 | 4 | | 3 | 2 | 24 | | 4 | 1 | 73 | | 5 | 4 | 124 | | 6 | 2 | 34 | | 7 | 1 | 75 | | 8 | 3 | 124 | +----------+--------------------------------+----------------- +
我想显示一个特定的所有贡献idea_id
,每个都显示列出的每个贡献的票数。
因此,对于idea_id = 1
,我希望看到以下输出:
+------------------+------------------+------------ ---+ | 贡献ID | 总喜欢 | 用户 ID | +------------------+------------------+------------ ---+ | 1 | 4 | 3 | | 2 | 2 | 4 | | 3 | 1 | 7 | | 4 | 1 | 10 | +------------------+------------------+------------ ---+
我目前正在使用以下查询,但正在显示奇怪的结果:
SELECT td_idea.idea_id,
td_idea_contribution.contributuion_id,
td_idea_contribution.submitter_id,
COUNT(td_idea_contribution_like.contribution_like_id) AS tot_like
FROM td_idea, td_idea_contribution, td_idea_contribution_like
WHERE td_idea.idea_id = td_idea_contribution.idea_id
AND td_idea_contribution.contribution_id = td_idea_contribution_like.contribution_id
AND td_idea.idea_id = '$id'
AND td_idea.status LIKE '%Design%'
ORDER BY tot_like DESC
编辑
我没有收到错误,我只是收到包含一条记录的异常结果,如下所示:
+------------------+------------------+------------ ---+ | 贡献ID | 总喜欢 | 用户 ID | +------------------+------------------+------------ ---+ | 1 | 8 | 3 | +------------------+------------------+------------ ---+