select
((select
COALESCE(sum(b.points_received), 0) as badge_total_points
from
user_badges ub
join
badges b ON ub.badge_id = b.badge_id
where
ub.user_id = '$user_id') + (select
COALESCE(sum(aps.given_points), 0) as total_action_points
from
user_action_points uap
join
action_point_system aps ON uap.point_id = aps.point_id
where
uap.user_id = '$user_id')) as total_contribution_points
how do I retrieve a list getting the total points grouped by a user_id with this SQL statement? Anybody have suggestions?
EDIT:
select
ub.user_id,
COALESCE(sum(b.isGold), 0) as gold_count,
COALESCE(sum(b.isSilver), 0) as silver_count,
COALESCE(sum(b.isBronze), 0) as bronze_count
from
user_badges ub
join
badges b ON ub.badge_id = b.badge_id
group by ub.user_id
how can I add this query to the result of the first question?
Although there are ways to do what you want, you can build on your existing query using correlated subqueries:
select u.*,
((select COALESCE(sum(b.points_received), 0) as badge_total_points
from user_badges ub join
badges b
ON ub.badge_id = b.badge_id
where ub.user_id = u.user_id
) +
(select COALESCE(sum(aps.given_points), 0) as total_action_points
from user_action_points uap join
action_point_system aps
ON uap.point_id = aps.point_id
where uap.user_id = u.user_id
)
) as total_contribution_points
from users u
order by total_contribution_points desc;
Related
I am generating a Member Leaderboard based on some field values.. Using Below SQL i am getting those values...
SELECT ue1.user_id,
ue1.meta_value coins ,
ue3.meta_value user_rank ,
ue2.meta_value gems,
COUNT(ue4.user_id) quiz_count,
COUNT(ue5.user_id) video_watch_count,
ue1.meta_value + ue3.meta_value + ue2.meta_value total_points
FROM `wp_usermeta` as ue1
INNER JOIN `wp_usermeta` as ue2 ON ue2.user_id = ue1.user_id
AND ue1.meta_key = '_coin_points'
AND ue2.meta_key = '_gem_points'
INNER JOIN `wp_usermeta` as ue3 ON ue3.user_id = ue1.user_id
AND ue3.meta_key = '_user_rank'
INNER JOIN `wp_gamipress_user_earnings` as ue4 ON ue4.user_id = ue1.user_id
AND ue4.post_type = 'quiz-game-master'
INNER JOIN `wp_gamipress_user_earnings` as ue5 ON ue5.user_id = ue1.user_id
AND ue5.post_type = 'video-game-master'
WHERE ue1.meta_value > 0
AND ue2.meta_value > 0
GROUP BY ue1.user_id, ue1.meta_value, ue2.meta_value, ue3.meta_value, ue4.user_id, ue5.user_id
ORDER BY total_points DESC LIMIT 0, 50
What i am getting is... Not Expected Query Result
See? video_watch_count is copying quiz_count value.. I don't know why? Would appreciate if you could help me out here.
Check you join statement. For both quiz_count, video_watch_count, you select data from table wp_gamipress_user_earnings. For each entry if join condition satisfied it will return user_id from corresponding table.
Finally you group it using user_id which is common to both join results.That's why you get same count.
I have a MySQL query and it takes about 25 sec. There are not many rows (just about 200) but I don't understand why it takes long time.
Query:
SELECT *
, c.id c_id
FROM campaign c
JOIN campaign_category cc
ON c.campaign_type = cc.id
WHERE c.is_deleted = 0
AND c.status = 1
AND c.id NOT IN (SELECT campaign_id FROM user_reviews WHERE user_id = 4)
AND c.amt_req > (SELECT COUNT(id)
FROM reserved_reviews
WHERE camping_id = c.id
AND user_id != 4)
+ (SELECT COUNT(id)
FROM user_reviews
WHERE campaign_id = c.id)
Edit:
I tried with JOIN like this but i got no result:
SELECT
*, `c`.`id` as `c_id`,COUNT(`ur`.`id`) as `total_reviewed`, COUNT(`rr`.`id`) as `total_reserved`
FROM
`campaign` `c`
JOIN `campaign_category` `cc` ON `c`.`campaign_type`=`cc`.`id`
JOIN `user_reviews` `ur` ON `ur`.`campaign_id`=`c`.`id`
JOIN `reserved_reviews` `rr` ON `rr`.`camping_id`=`c`.`id`
WHERE
`c`.`is_deleted` =0
AND
`c`.`status` = 1
AND
`ur`.`user_id` != 4
GROUP BY `c`.`id`
HAVING `c`.`amt_req` > COUNT(`ur`.`id`) + COUNT(`rr`.`id`)
Edit: Table structures: First Image - user_reviews Table, Second image campagin Table, Third image: reserved_reviews Table.
http://imgur.com/GI4817B,SdnSxuz,truxHM6#0
You can improve this query with indexes;
SELECT *, c.id c_id
FROM campaign c JOIN
campaign_category cc
ON c.campaign_type = cc.id
WHERE c.is_deleted = 0 AND
c.status = 1 AND
c.id NOT IN (SELECT campaign_id FROM user_reviews WHERE user_id = 4)
c.amt_req > (SELECT COUNT(*)
FROM reserved_reviews
WHERE campaign_id = c.id AND user_id <> 4)
) +
(SELECT COUNT(id)
FROM user_reviews
WHERE campaign_id = c.id
) ;
For the outer query and joins: campaign(status, is_deleted, id, amt_req) and campaign_category(id) (you should have the latter if it is defined as a primary key.
Then: user_reviews(user_id, campaign_id), reserved_reviews(campaign_id, user_id), and user_reviews(campaign_id).
I have 1 table with string type. (photo_add, photo_comment etc.)
Then I have 4 other tables that are connected with these types.
I need to make a query which will choose the data from all these tables according to the type.
I created this but it does not work.
SELECT DISTINCT a.id, a.event, a.params, a.created_at, a.item_id, a.sender_id, a.receiver_id, u.name, u.id as userid
FROM `eva49_lovefactory_activity` a, eva49_users u
CASE a.event
WHEN "photo_add" THEN
INNER JOIN eva49_lovefactory_photos lp ON lp.id = a.item_id AND lp.fsk18 = -1
WHEN "group_create" THEN
INNER JOIN eva49_lovefactory_groups lg ON lg.id = a.item_id AND lg.fsk18 = -1
WHEN "photo_comment" THEN
INNER JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND lic.fsk18 = -1
WHEN "profile_comment" THEN
INNER JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND lic.fsk18 = -1
WHEN "profile_comment" THEN
INNER JOIN eva49_lovefactory_profiles lp ON lp.user_id = a.receiver_id AND lp.status_fsk18 = -1
ELSE 1=1
END
WHERE (u.id = a.sender_id OR u.id = a.receiver_id) and (u.id <> 379)
ORDER BY a.created_at DESC LIMIT 0, 10
Is there any way how to pair the tables? The database structure was given to me like this.
EDIT:
There is a table "eva49_lovefactory_activity" which contains information about recent activity. It has a column "event" that contains string with the event name (photo_add, photo_comment etc.)
There are other tables - eva49_lovefactory_photos, eva49_lovefactory_profiles, eva49_lovefactory_groups etc. In these tables I have to find whether the item was approved by admin. If it was I can show it. (fsk18 = -1)
Move CASE condition to JOIN condition and do LEFT JOIN..
SELECT DISTINCT a.id, a.event, a.params, a.created_at, a.item_id, a.sender_id, a.receiver_id, u.name, u.id as userid
FROM `eva49_lovefactory_activity` a, eva49_users u
LEFT JOIN eva49_lovefactory_photos lp ON lp.id = a.item_id AND lp.fsk18 = -1
AND a.event='photo_add'
LEFT JOIN eva49_lovefactory_groups lg ON lg.id = a.item_id AND lg.fsk18 = -1
AND a.event='group_create'
LEFT JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND
lic.fsk18 = -1 AND a.event='photo_comment'
LEFT JOIN eva49_lovefactory_item_comments lic ON lic.item_id = a.item_id AND
lic.fsk18 = -1 AND a.event='profile_comment'
WHERE (u.id = a.sender_id OR u.id = a.receiver_id) and (u.id <> 379)
ORDER BY a.created_at DESC LIMIT 0, 10
I have three tables:
cp_projeto (id, nome...)
cp_habilidade_projeto (id, id_projeto)
cp_habilidade (id, nome...)
I need all projects with all cp_habilidade where cp_projeto have one cp_habilidade. My actual query:
SELECT
p.id as id_projeto,
p.nome as nome_projeto,
p.id_tipo_projeto,
p.dhPostagem,
cp_habilidade_projeto.id as id_habilidade_projeto,
cp_habilidade.nome as nome_habilidade
FROM (
SELECT * FROM cp_projeto
WHERE (id_status_projeto = 2)
ORDER BY dhPostagem DESC LIMIT 0, 10
) AS p
inner JOIN cp_habilidade_projeto ON (cp_habilidade_projeto.id_projeto = p.id)
inner JOIN cp_habilidade ON (cp_habilidade.id = cp_habilidade_projeto.id_habilidade)
JOIN cp_sub_categoria ON (cp_sub_categoria.id = p.id_sub_categoria)
WHERE (
p.nome like '%CSS%'
OR cp_habilidade.nome like '%CSS%'
)
This returns only cp_habilidade.nome = %CSS%, I need it all.
Thanks!
SELECT
p.id as id_projeto,
p.nome as nome_projeto,
p.id_tipo_projeto,
p.dhPostagem,
hp.id as id_habilidade_projeto,
h.nome as nome_habilidade
FROM cp_projecto p
JOIN cp_habilidade_projeto hp ON p.id = hp.id_projeto
JOIN cp_habilidade h ON h.id = hp.id_habilidade
WHERE p.id IN ( SELECT cp_habilidade_projeto.id_projeto
FROM cp_habilidade
JOIN cp_habilidade_projeto ON cp_habilidade.id = cp_habilidade_projeto.id_habilidade
WHERE cp_habilidade.nome LIKE '%CSS%' )
How can I adjust this JOIN clause so that rows with a NULL value for the CountLocId or CountNatId columns are returned in the result?
In other words, if there is no match in the local_ads table, I still want the user's result from the nat_ads table to be returned -- and vice-versa.
SELECT u.franchise, CountLocId, TotalPrice, CountNatId, TotalNMoney, (
TotalPrice + TotalNMoney
)TotalRev
FROM users u
LEFT JOIN local_rev lr ON u.user_id = lr.user_id
LEFT JOIN (
SELECT lrr_id, COUNT( lad_id ) CountLocId, SUM( price ) TotalPrice
FROM local_ads
GROUP BY lrr_id
)la ON lr.lrr_id = la.lrr_id
LEFT JOIN nat_rev nr ON u.user_id = nr.user_id
INNER JOIN (
SELECT nrr_id, COUNT( nad_id ) CountNatId, SUM( tmoney ) TotalNMoney
FROM nat_ads
WHERE MONTH = 'April'
GROUP BY nrr_id
)na ON nr.nrr_id = na.nrr_id
WHERE lr.month = 'April'
AND franchise != 'Corporate'
ORDER BY franchise
Thanks in advance for your help!
try the following in where clause while making a left join. This will take all rows from right table with matched condition
eg.
LEFT JOIN local_rev lr ON (u.user_id = lr.user_id) or (u.user_id IS NULL)
Use this template, as it ensures that :
you have only one record per user_id (notice all subquerys have a GROUP BY user_id) so for one record on user table you have one (or none) record on subquery
independent joins (and calculated data) are not messed togeder
-
SELECT u.franchise, one.CountLocId, one.TotalPrice, two.CountNatId, two.TotalNMoney, (COALESCE(one.TotalPrice,0) + COALESCE(two.TotalNMoney,0)) TotalRev
FROM users u
LEFT JOIN (
SELECT x.user_id, sum(xORy.whatever) as TotalPrice, count(xORy.whatever) as CountLocId
FROM x -- where x is local_rev or local_ads I dont know
LEFT JOIN y on x.... = y.... -- where y is local_rev or local_ads I dont know
GROUP BY x.user_id
) as one on u.user_id = one.user_id
LEFT JOIN (
SELECT x.user_id, sum(xORy.whatever) as TotalNMoney, count(xORy.whatever) as CountNatId
FROM x -- where x is nat_rev or nat_ads I dont know
LEFT JOIN y on x.... = y.... -- where y is nat_rev or nat_ads I dont know
GROUP BY x.user_id
) as two on u.user_id = two.user_id