2011-03-12 103 views
0

我試圖從brewsrecipes表中獲取信息基於表中跟隨有多少追隨者,但我得到一個SQL錯誤,因爲我正在返回我的查詢中有多個結果。我應該把它分成兩個查詢嗎?或者是有作爲變通,讓我在我的外WHERE聲明SQL查詢與查詢返回多個結果

SELECT 
    b.bid AS bid, b.name AS bname, 
    r.name AS recipe, b.status AS status, 
    r.rid, a.f_name, a.l_name, a.pic_loc, a.uid AS uid 
FROM 
    recipes r, brews b, account a 
WHERE 
    b.uid = (SELECT followed FROM follow 
    WHERE follower = '#cookie.id#') 
    AND b.rid = r.rid AND b.uid = a.uid 
GROUP BY b.bid 
ORDER BY b.time DESC 

回答

3

檢查多個b.uid只需使用WHERE b.uid IN (SELECT followed...

0

爲了返回多個結果集,你需要使用CFSTOREDPROC,但這不是你的問題。在您的where子句中將您的「=」符號更改爲IN,或將您的選擇更改爲加入。你的問題只是一個錯誤的查詢,而不是多個結果集。

SELECT 
    b.bid AS bid, b.name AS bname, r.name AS recipe, 
    b.status AS status, r.rid, a.f_name, a.l_name, 
    a.pic_loc, a.uid AS uid 
FROM 
    recipes AS r 
    LEFT JOIN brews AS b 
     ON b.rid = r.rid 
    LEFT JOIN account AS a 
     ON b.uid = a.uid 
    LEFT JOIN follow AS f 
     ON f.followed = b.uid 
WHERE f.follower = <cfqueryparam cfsqltype="CF_SQL_VARCHAR" value="#cookie.id#" /> 
GROUP BY b.bid 
ORDER BY b.time DESC 
0

GROUP BY子句可能也會導致您遇到問題。如果你正在使用聚合函數(SUM(),AVG(),MIN(),MAX()等等),你應該只在你的查詢中有一個GROUP BY子句,並且需要對你正在合計的記錄進行分組,等等分成子組。