2014-03-13 32 views
0

我有兩個sql查詢。我想知道如何將兩個查詢合併爲一個。在MySQL中使用子查詢進行選擇

// query #1 - Using this query return totalpoints of specific user ex :userId (10234324234) 
SELECT SUM(points) AS totalPoints FROM users_points WHERE user_fb_id =10234324234 AND match_id >= 2 

//query #2 - Using this query will return all the records on specific condition 
SELECT users_points.*, user_points.totalPoints, user.user_firstName FROM users_points INNER JOIN user ON users_points.user_fb_id = user.user_fb_id WHERE users_points.match_id >= '$matchId' 

我想獲取記錄,所有的用戶記錄與每個用戶的totalPoints使用一個查詢。

+0

http://dev.mysql.com/doc/refman/5.0 /en/subqueries.html – Scuzzy

+0

@Scuzzy我嘗試了幾種方法。但我不能那樣做。你可以評論相關的查詢或一個例子 –

回答

0

只是這樣做......

SELECT * FROM (SELECT * FROM B WHERE my_condition = something) WHERE my_second_condition = something_else 

如果我得到了正確的順序應該你這個樣子

SELECT SUM(points) AS totalPoints FROM (SELECT users_points.*, user_points.totalPoints, user.user_firstName FROM users_points INNER JOIN user ON users_points.user_fb_id = user.user_fb_id WHERE users_points.match_id >= '$matchId') WHERE user_fb_id =10234324234 AND match_id >= 2 
+0

我試過你的查詢直接。有錯誤說需要一個別名。我添加了別名作爲測試(SELECT SUM(points)AS totalPoints FROM(SELECT users_points。*,users_points.totalPoints,user.user_firstName FROM users_points INNER JOIN user ON users_points.user_fb_id = user.user_fb_id WHERE users_points.match_id> = 2)as test WHERE user_fb_id = 10234324234 AND match_id> = 2)運行此qury後,我得到#1054 - '字段列表'中的未知列'users_points.totalPoints' –