POSTS
[ id - post - content ]
----------------------------
[ 1 - 1post1 - 1content1]
[ 2 - 1post2 - 2content2]
VOTES
[ id - pid]
------------
[ 1 - 1]
[ 2 - 1]
[ 3 - 1]
[ 4 - 2]
[ 5 - 2]
1post1 = 3 votes
2post2 = 2 votes
當我嘗試此查詢ORDER BY和別名COUNT()在一個查詢
$query = "SELECT a.title, a.content, b.COUNT(id) FROM posts a, votes b WHERE id = :id ORDER BY b.COUNT(id)";
它不工作,並給了我這個錯誤
FUNCTION b.COUNT does not exist. Check the 'Function Name Parsing and Resolution
是否有一個更好的如何做到這一點?我想過使用UNION ALL
是這樣
$query = "SELECT title, content, COUNT(id) FROM posts WHERE id = :id
UNION ALL
SELECT COUNT(id) FROM votes WHERE pid = :id";
,但我將無法使用ORDER BY COUNT(id)
的第一部分,這是我想訂購取決於第二部分問題就在這裏。
要清楚,你是否試圖選擇按'票數'排序的'posts'?什麼是表結構? –
你是指'count(b.id)'? – xQbert
@NiettheDarkAbsol推出了一個示例表。 –