[英]Mysql: Select all data between specified points
I have a mysql tables with data connected to points. 我有一个将数据连接到点的mysql表。 like this: 像这样:
tbl_user tbl_user
tbl_earned_points tbl_earned_points
tbl_used_points tbl_used_points
I have a mysql query that select data of available points. 我有一个mysql查询,它选择可用点的数据。 This is the query: 这是查询:
SELECT
user.user_email_id AS userEmailId,
IFNULL(SUM(earn_points.earned_points),0) AS lifeTimePoints,
IFNULL((SUM(earn_points.earned_points) - (SELECT IFNULL(SUM(used_points.points_used),0) FROM tbl_used_points AS used_points WHERE used_points.user_id=earn_points.user_id)),0) AS availablePoints
FROM tbl_earned_points AS earn_points
RIGHT JOIN tbl_user AS user ON earn_points.user_id=user.user_id WHERE user.user_email_id <> '' AND user.user_email_id <> '0'
GROUP BY user.user_email_id
ORDER BY availablePoints ASC
From above query i am getting all emails & result like 从上面的查询中,我收到所有电子邮件和类似的结果
userEmailId lifeTimePoints availablePoints
samal07@gmail.com 1745 1500
mistyorr_20@yahoo.com 100 75
bsfar@yahoo.com 85 85
tgray@gmail.com 94 90
lori_bag@yahoo.com 547 450
My problem is that I only need to get the rows availablePoints between 80 TO 99 . 我的问题是,我只需要获取80至99之间的可用点即可 。
So my query would result like 所以我的查询结果是
userEmailId lifeTimePoints availablePoints
bsfar@yahoo.com 85 85
tgray@gmail.com 94 90
只需将其添加到您的WHERE子句中:
AND availablePoints BETWEEN 80 AND 99
you can use BETWEEN
clause for availablePoints
. 您可以将BETWEEN
子句用于availablePoints
。 Set your availablePoints
whatever you want. 随意设置您的availablePoints
积分。
Essentially you just need to add WHERE
criteria. 本质上,您只需要添加WHERE
条件。 However, you can also rewrite your query to remove the correlated subquery and just use OUTER JOINs
: 但是,您也可以重写查询以删除相关的子查询,而只需使用OUTER JOINs
:
SELECT u.user_email,
COALESCE(e.points,0) lifeTimePoints,
COALESCE(a.points,0) availablePoints
FROM tbl_user AS u
LEFT JOIN (
SELECT SUM(earned_points) points, user_id
FROM tbl_earned_points
GROUP BY user_id
) e ON u.user_id = e.user_id
LEFT JOIN (
SELECT SUM(points_used) points, user_id
FROM tbl_used_points
GROUP BY user_id
) a ON u.user_id = a.user_id
WHERE a.points between 80 and 99
If you'd prefer to use your existing query, you'll either need to wrap it in a subquery and use the column alias ( availablepoints
), or you'll need to use the entire IFNULL
statement in your WHERE
criteria. 如果您希望使用现有查询,则需要将其包装在子查询中并使用列别名( availablepoints
),或者需要在WHERE
条件中使用整个IFNULL
语句。 You cannot reference the column alias directly. 您不能直接引用列别名。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.