在MySQL中选择子查询

时间:2014-03-13 21:28:06

标签: php mysql sql

我有两个SQL查询。我想知道如何将两个查询合并为一个。

// 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使用一个查询。

1 个答案:

答案 0 :(得分: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