2013-12-09 64 views
0

我正在爲一個類設計一個簡單的微博網站,並且需要我的滾動分頁的sql語句幫助。從其他表中排序的SQL語句使用NOT IN

我有2個表格:用戶和關注。

在我的'用戶'表中我有uid,名字,姓氏,電子郵件和用戶名。

我有另一個具有rid,FollowedName和FollowingName的'Follows'表。例如,如果用戶名「Alex」後跟用戶名「Bob」,那麼會有一個新的行,其中FollowedName =「Bob」和FollowingName =「Alex」。

對於搜索頁面,我允許用戶根據流行度(基於關注者數量)進行排序。所以我需要一個SQL語句來選擇'User'中的所有行,然後根據每個用戶擁有的'Follows'表中有多少條目來排序它們。

最重要的是,我需要該語句來過濾那些uid在字符串名稱$ explodedids中的用戶。

我所擁有的一切工作,但我無法弄清楚在何處放置"WHERE uid NOT IN (".$explodedids.")".

這裏是我的發言返回正常,但不會過濾掉$ explodedids:

"SELECT Username, FirstName, LastName, Email, COUNT(Followingname) AS count 
FROM (SELECT u.Username, u.FirstName, u.LastName, u.Email, f.Followingname 
     FROM User AS u LEFT JOIN Follows AS f ON u.Username = f.Followingname) 
AS T GROUP BY Username ORDER BY count DESC LIMIT ".$postnumbers 

$ postnumbers僅僅是極限我的滾動分頁數。我相信我剛剛將WHERE NOT IN放在錯誤的地方,但如果你們能幫助我,那就太棒了。

回答

1

這裏是一個select語句,將滿足您的要求:

--Test Data 
CREATE TABLE #User ( uid int 
        , FirstName varchar(50) 
        , LastName varchar(50) 
        , Email varchar(50) 
        , Username varchar(50)); 

INSERT INTO #User(uid, FirstName, LastName, Email, Username) VALUES(1,'MackF','MackL','[email protected]','MackUname'); 
INSERT INTO #User(uid, FirstName, LastName, Email, Username) VALUES(2,'2F','2L','[email protected]','2Uname'); 
INSERT INTO #User(uid, FirstName, LastName, Email, Username) VALUES(3,'3F','3L','[email protected]','3Uname'); 
INSERT INTO #User(uid, FirstName, LastName, Email, Username) VALUES(4,'4F','4L','[email protected]','4Uname'); 

CREATE TABLE #Follows(rid int 
        , FollowedName varchar(50) 
        , FollowingName varchar(50)); 

INSERT INTO #Follows VALUES(1,'4Uname','2Uname'); 
INSERT INTO #Follows VALUES(2,'MackUname','4Uname'); 
INSERT INTO #Follows VALUES(3,'4Uname','3Uname'); 
INSERT INTO #Follows VALUES(4,'MackUname','3Uname'); 

--Query 
SELECT u.Username 
    , u.FirstName 
    , u.LastName 
    , u.Email 
    , COUNT(f.Followingname) AS followercount 
FROM #User u 
LEFT JOIN #Follows f 
    ON u.Username = f.Followingname 
WHERE uid NOT IN (2,4)--here is where your WHERE clause goes remove this line to see all records 
GROUP BY u.Username 
ORDER BY followercount DESC 

--Results 
USERNAME FIRSTNAME LASTNAME EMAIL   FOLLOWERCOUNT 
3Uname  3F   3L   [email protected]  2 
MackUname MackF  MackL  [email protected] 0 

這裏是一個SQL fiddle到嘗試一切。

注意:我已將您的列「count」更名爲「followercount」,因爲COUNT是一個保留字。

+0

這很接近,但它似乎沒有返回所有的元素。如果用戶沒有追隨者,這會工作嗎? – user3060454

+0

看看這個SQL小提琴:http://sqlfiddle.com/#!2/083f8/11它顯示了一個例子,只是...我認爲並非所有元素都返回的原因可能是WHERE uid NOT IN( 2,4) – Mack

+0

我是一個白癡,我誤算了它一直在工作的用戶數量。謝謝您的幫助! – user3060454

1
"SELECT Username, FirstName, LastName, Email, 
COUNT(Followingname) AS count 
FROM (
SELECT u.Username, u.FirstName, u.LastName, u.Email, f.Followingname 
     FROM User AS u 
     LEFT JOIN Follows AS f 
     ON u.Username = f.Followingname 
     WHERE u.id NOT IN($explodids) 
) AS T 
GROUP BY Username 
ORDER BY count DESC 
LIMIT $postnumbers"; 

假設$ explodids是格式的字符串 'X,Y,Z'