i have the following sql query
SELECT
vmm_user.username,
vmm_songs.*,
vmm_albums.desc,
vmm_albums.release,
vmm_albums.name,
AVG(vmm_songrating.rating) AS ratingavg,
COUNT(vmm_songrating.id) AS ratingcount
FROM
vmm_songs
LEFT JOIN
vmm_user
ON
vmm_songs.userid=vmm_user.id
LEFT JOIN
vmm_albums
ON
vmm_songs.albumid=vmm_albums.id
LEFT JOIN
vmm_songrating
ON
vmm_songs.id=vmm_songrating.songid
GROUP BY
vmm_songs.id
HAVING
COUNT(vmm_songrating.id) >= 2
ORDER BY
AVG(vmm_songrating.rating) DESC
LIMIT
10
this works fine but now i have to know if a user already voted for a song and thats my problem
the rating table looks like this
id|songid|userid|rating
i tried something like this
SELECT
...
COUNT(vmm_songrating.id) as hasvoted
...
OUTER JOIN
vmm_songrating
ON
vmm_songrating.userid = $id
...
$id is the user session id
but it doesnt work :/
you can try adding something like this to the selected fields
SUM(case when vmm_songrating.userid = $id then 1 end) as hasvoted
Related
I have a problem with this mysql query. When I run it in phpmyadmin it shows only the "player_11" and the "player_11Count" . I can't join the players table. There are no errors shown. It acts just like the JOIN is not there. Do you have any ideas?
SELECT player_11, COUNT(player_11) AS player_11Count
FROM user_teams
LEFT JOIN players ON user_teams.player_11 = players.player_id
WHERE round_id = '31' && user_teams.team_id = '22'
GROUP BY player_11
ORDER BY COUNT(player_11) DESC
You are saying to mysql "show me only player_11 and count(player_11)". You can't have anything else even with joining tables. Joining tables doesn't automatically select fields (unless your are using SELECT *)
You should say:
SELECT players.*, user_teams.player_11, COUNT(user_teams.player_11) AS player_11Count
FROM user_teams .....
Then your players data will show up
I am trying to write a query. I got it work half way, but I am having problems with the LEFT JOIN.
I have three tables:
user
user_preferences
user_subscription_plan
User will always have one user_preference, but it can have many or no entries in the user_subscription_plan
If the user has no entry in the user_subscription_plan, or if he has only one then my sql works. If I have more then one, then I have issue. In the case of two entries, how can I make it to return the last one entered? I tried playing with ORDER statement, but it does not work as expected. Somehow I get empty rows.
Here is my query:
SELECT u.id AS GYM_USER_ID, subscription_plan.id AS subscriptionId, up.onboarding_completed AS CompletedOnboarding,
(CASE
WHEN ((up.onboarding_completed = 1)
AND (ISNULL(subscription_plan.id)))
THEN 'freemiun'
WHEN (ISNULL(up.onboarding_completed)
AND (ISNULL(subscription_plan.id)))
THEN 'not_paying'
END) AS subscription_status
FROM user AS u
INNER JOIN user_preferences up ON up.user_id = u.id
LEFT JOIN (
SELECT * FROM user_subscription_plan AS usp ORDER BY usp.id DESC LIMIT 1
) AS subscription_plan ON subscription_plan.user_id = u.id
GROUP BY u.id;
If I run it as it is, then subscription_plan.id AS subscriptionId is always empty.
If I remove the LIMIT clause, then its not empty, but I am still getting the first entry, which is wrong in my case
I have more CASE's to cover, but I can't process until I solve this problem.
Please try to use "max(usp.id)" that "group by subscription_plan.user_id" instead of limit 1.
If you limit 1 in the subquery, the subquery's result will always return only 1 record (if the table has data).
So the above query can be rewritten like this.
Sorry, I didn't test, because I don't have data, but please try, hope this can help.
SELECT
u.id AS GYM_USER_ID,
subscription_plan.id AS subscriptionId,
up.onboarding_completed AS CompletedOnboarding,
(CASE
WHEN
((up.onboarding_completed = 1)
AND (ISNULL(subscription_plan.id)))
THEN
'freemiun'
WHEN
(ISNULL(up.onboarding_completed)
AND (ISNULL(subscription_plan.id)))
THEN
'not_paying'
END) AS subscription_status
FROM
user AS u
INNER JOIN
user_preferences up ON up.user_id = u.id
LEFT JOIN
(SELECT
usp.user_id, MAX(usp.id)AS id
FROM
user_subscription_plan AS usp
GROUP BY usp.user_id) AS subscription_plan ON subscription_plan.user_id = u.id;
I have a MySQL query that outputs to a php table but I'm having issues in joining two tables that both use a COUNT:
$query = "SELECT mqe.registration,
COUNT(*) AS numberofenqs,
COUNT(DISTINCT ucv.ip) AS unique_views,
SUM(ucv.views) AS total_views
FROM main_quick_enquiries AS mqe
LEFT OUTER JOIN used_car_views AS ucv
ON ucv.numberplate = mqe.registration
WHERE mqe.registration IS NOT NULL
GROUP BY mqe.registration ORDER BY numberofenqs DESC";
The query runs, but the number within the numberofenqs column is always wrong as i know from performing that query on its own that it comes in with the correct result:
SELECT registration, COUNT(*) AS numberofenqs FROM main_quick_enquiries GROUP BY registration ORDER BY numberofenqs DESC
Why is the COUNT(*) not working correctly in top query code and where is it getting the figures from?
it could be because of LEFT OUTER JOIN ...
Try to run this:
SELECT registration
, count(*)
FROM main_quick_enquiries
GROUP BY registration
and compare it with this result
SELECT mqe.registration
, count(*)
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
GROUP BY mqe.registration
There could be a problem :) in duplicity rows... try to find one specific registration number, and compare the details of both query
SELECT *
FROM main_quick_enquiries
WHERE registration = XXXX
+
SELECT *
FROM main_quick_enquiries mqe
LEFT OUTER JOIN used_car_views ucv
ON ucv.numberplate = mqe.registration
WHERE registration = XXXX
you should see the diffs
Thanks All, but I think I've nailed it with COUNT(DISTINCT mqe.id) instead of COUNT(*).
I have a forum with two tables forum_posts and forum_replies. I need to get the most active users. Here is what I recently tried
SELECT forum_reply.added_by, forum_posts.added_by FROM forum_reply LEFT JOIN forum_posts ON forum_posts.added_by = forum_reply.added_by
UNION
SELECT forum_posts.added_by, forum_reply.added_by FROM forum_posts RIGHT JOIN forum_reply ON forum_reply.added_by = forum_posts.added_by
WHERE forum_reply.date_added < '".$now."' AND forum_reply.date_added > '".$past24h."' AND forum_posts.date_added < '".$now."' AND forum_posts.date_added > '".$past24h."'
GROUP BY forum_reply.added_by, forum_posts.added_by ORDER BY COUNT(*) DESC LIMIT 5");
The problem is this query gives me only one result instead of 5. And the result is definitely not real because the user this query gives me haven't posted anything for the last 24 hours.
I've got my tables posts and user_unread_posts.
In posts, all the posts of a forum are saved, and in user_unread_posts all posts are saved which are read by a user.
user_undread_postslooks like this:
id uid pid
Now I want to allow users to open a random post which they haven't read. I've tried something like
SELECT * FROM posts
LEFT JOIN user_unread_posts uup
ON uup.pid=posts.id
WHERE uup.uid<>1
ORDER BY RAND()
LIMIT 1
(Whilst 1 is a placeholder UID)
But it doesn't work, like it should work, it return posts too, which are read... How can I fix that?
SELECT *
FROM posts
WHERE id NOT IN
(
SELECT pid
FROM user_unread_posts uup
WHERE uid = $myuserid
)
ORDER BY
RAND()
LIMIT 1
You wanted to use IS NULL with the LEFT JOIN. Using <> turns the LEFT JOIN into an INNER JOIN because NULL can never match the <> operator.
Here is a corrected query:
SELECT * FROM posts
LEFT JOIN user_unread_posts uup
ON uup.pid=posts.id
WHERE uup.uid IS NULL
ORDER BY RAND()
LIMIT 1