现在我有这个问题:
SELECT s.*, u.username,
FROM submissions s
JOIN users u ON s.user_id
WHERE s.id = 15
这给了我:
*************************** 1. row ***************************
id: 15
title: dsaffdsadfsafds
slug: dsaffdsadfsafds
description: dfsdfsdfsafadsdfasdfs
user_id: 15
created: 2014-01-12 14:21:29
category: autos-vehicles
type: request
thumbnail:
removed: 0
keywords: dsaffdsadfsafds
ip: 127001
username: blahbster
现在我想从另一个名为submissions_votes
的表中添加一个计数。
SELECT count(id) as "submissionVotes"
FROM submissions_votes
WHERE submission_id = 15
我试图将它们组合起来:
SELECT s.*, u.username,
(SELECT count(id)
FROM submissions_votes
WHERE submission_id = 15) as "submissionVotes",
FROM submissions s
JOIN users u ON s.user_id
WHERE s.id = 15;
但我收到了一个错误。这里需要一些帮助...
答案 0 :(得分:0)
尝试:
SELECT s.*, u.username, COUNT(sv.id) votes FROM submissions s INNER JOIN users u ON
s.user_id = u.id LEFT JOIN submissions_votes sv ON s.id = sv.submission_id
WHERE s.id = 15 GROUP BY sv.submission_id