How to get all posts from each user? - php

I have the table users with information about every user. Then the table posts with information about articles and finally the table user_posts, which contains following columns:
user_id
post_id
...
I am trying to get the chart of users with the highest count of posts. I made this query:
SELECT u.id as uid, u.name as uname,
count(up.id) as up_count
FROM users as u JOIN user_posts as up ON up.user_id = u.id ORDER BY vcount DESC LIMIT 25
This query returns me only one user and the total count of all rows in the table user_posts.
What am I doing wrong? I need to get the list of 25 users sorted by the count of articles that published each user.
Thank you in advance

Your query needs to have GROUP BY clause because you have used COUNT() function.
SELECT u.id as uid,
u.name as uname,
count(up.id) as up_count
FROM users as u
LEFT JOIN user_posts as up
ON up.user_id = u.id
GROUP BY u.id, u.name
ORDER BY up_count DESC LIMIT 25
You must have grouped them by ID otherwise you'll single total count result for all records. One more thing, use LEFT JOIN so even users with no post still will be visible in you result with the score of 0.

SELECT
u.id as uid, u.name as uname, count(up.id) as up_count
FROM users as u
JOIN user_posts as up ON up.user_id = u.id
GROUP BY
u.id, u.name
ORDER BY
vcount
DESC LIMIT 25

Related

EXISTS query optimization on mysql query

I have a big data problem with MySQL.
I have:
a users table with 59033 rows, and
a user_notes table with 8753 rows.
But when I search which users have user note in some dates.
My query like this :
SELECT u.*, rep.name as rep_name FROM users as u
LEFT JOIN users as rep on rep.id = u.add_user
LEFT JOIN authorization on authorization.id = u.authorization
LEFT JOIN user_situation_list on user_situation_list.user_situation_id = u.user_situation
WHERE
EXISTS(
select * from user_notes
where user_notes.note_user_id = u.id AND user_notes.create_date
BETWEEN "2017-10-20" AND "2017-10-22"
)
ORDER BY u.lp_modify_date DESC, u.id DESC
Turn it around -- find the ids first; deal with the joins later.
SELECT u.*,
( SELECT rep.name
FROM users AS rep
WHERE rep.id = u.add_user ) AS rep_name
FROM (
SELECT DISTINCT note_user_id
FROM user_notes
WHERE create_date >= "2017-10-20"
AND create_date < "2017-10-20" + INTERVAL 3 DAY
) AS un
JOIN users AS u ON u.id = un.note_user_id
ORDER BY lp_modify_date DESC, id DESC
Notes
No GROUP BY needed;
2 tables seem to be unused; I removed them;
I changed the date range;
User notes needs INDEX(create_date, note_user_id);
Notice how I turned a LEFT JOIN into a subquery in the SELECT list.
If there can be multiple rep_names, then the original query is "wrong" in that the GROUP BY will pick a random name. My Answer can be 'fixed' by changing rep.name to one of these:
MAX(rep.name) -- deliver only one; arbitrarily the max
GROUP_CONCAT(rep.name) -- deliver a commalist of names
Rewriting your query to use a JOIN rather than an EXISTS check in the where should speed it up. If you then group the results by the user.id it should give you the same result:
SELECT u.*, rep.name as rep_name FROM users as u
LEFT JOIN users as rep on rep.id = u.add_user
LEFT JOIN authorization on authorization.id = u.authorization
LEFT JOIN user_situation_list on user_situation_list.user_situation_id = u.user_situation
JOIN user_notes AS un
ON un.note_user_id
AND un.create_date BETWEEN "2017-10-20" AND "2017-10-22"
GROUP BY u.id
ORDER BY u.lp_modify_date DESC, u.id DESC

SQL Query, cannot find the right query

I'm hosting a platform where users get SQL logs, I want to make some kind of highscore for it so there like a top 10 of users who got the most logs.
All registered users get their own 'ID', in the logs the user 'ID' is displayed as: 'id'. So in users 'ID' is in capitals and in 'logs' it is without capitals.
I would like to have some kind of query like this:
SELECT username,ID FROM users ORDER BY COUNT (id) FROM logs LIMIT 10;
But this does not work so maybe someone of you have a solution?
Try this, you'll need to join the table and use the group function, really useful for what you're trying to achieve:
SELECT users.username, users.id
FROM users
LEFT JOIN logs on users.id = logs.id
GROUP BY users.id
ORDER BY COUNT (logs.id)
LIMIT 10;
Or try with aliases:
SELECT users.username as u, users.id as i
FROM users
LEFT JOIN logs on users.id = logs.id
GROUP BY i
ORDER BY COUNT (logs.id)
LIMIT 10;
Try this to achieve the log count:
SELECT users.username, users.id, COUNT(logs.id) as quantity
FROM users
LEFT JOIN logs on users.id = logs.id
GROUP BY users.id
ORDER BY COUNT(logs.id)
LIMIT 10;
Simple Inner Join should work
SELECT U.username,U.ID
FROM users U
INNER JOIN logs L
on U.id = L.id
Group by U.username,U.ID
ORDER BY COUNT (id) FROM LIMIT 10
SELECT User.username, User.ID, Count(Log.id) LogCount
FROM users User
LEFT JOIN logs Log
ON User.ID = Log.id
GROUP BY User.ID, User.username
ORDER BY LogCount DESC
LIMIT 10

Select a row from two tables depend on primary key(Mysql)?

I have two table users and album. In users table there is user_id primary key .In other table albums there are multiple rows with that user_id because every time when a user upload a new album it uploads with user_id as foreign key. I want to select only once the user_id with other table(album) ignore other result set.
How can I achieve this?
SELECT a.*, b.*
FROM users a
INNER JOIN album b
ON a.user_ID = b.user_ID
INNER JOIN
(
SELECT user_ID, MAX(photo_id) max_rec
FROM album
GROUP BY user_ID
) c ON b.user_ID = c.user_ID AND
b.photo_id = c.max_rec
SELECT album.* FROM album LEFT JOIN users ON user.id = album.id WHERE user.id = SOMEIDHERE
I believe this will work, your not giving me a whole lot of info to work with.
SELECT *
FROM ( SELECT u.*, a.*
FROM users AS u
INNER JOIN album AS a
ON u.user_ID = a.user_ID
ORDER BY a.created DESC) AS h
GROUP BY user_ID
ORDER BY b.created DESC -> ORDER BY whatever row you wish for. In this case the newest one is chosen.

MySQL Query Multiple LEFT Joins problem

I am trying to get the required result from the following query but it doesnt seem to work...
SELECT DISTINCT
u.user_name as user_name,
u.total_points as total_points,
u.user_id as user_id,
COUNT(a.id) as user_total_articles_published,
COUNT(r.id) as user_total_replies_published,
COUNT(v.id) as user_total_votes_done
FROM users as u
LEFT JOIN articles as a ON u.user_id=a.user_id
LEFT JOIN replies as r ON u.user_id=r.user_id
LEFT JOIN votes as v ON u.user_id=v.user_id
GROUP BY u.user_id
ORDER BY u.total_points DESC
LIMIT 10
If i remove the last 2 LEFT jOINS the query will work... whats wrong with the other 2? Do i have to use another method for this to work?
thanks
I think by 'not working' you mean that the query returns too many records? That is because of the combination of joins. You return each reply for each article record, so the numbers are multiplied. You can solve this by using DISTINCT in the COUNT. That way, you count the unique id's, so you count each article only once:
COUNT(distinct a.id) as user_total_articles_published,
COUNT(distinct r.id) as user_total_replies_published,
COUNT(distinct v.id) as user_total_votes_done
[edit]
A possibly faster solution, eliminating the need for DISTINCT and GROUP BY:
SELECT
u.user_name as user_name,
u.total_points as total_points,
u.user_id as user_id,
(SELECT COUNT(a.id) FROM articles a
WHERE a.user_id = u.user_id) as user_total_articles_published,
(SELECT COUNT(r.id) FROM replies r
WHERE r.user_id = u.user_id) as user_total_replies_published,
(SELECT COUNT(v.id) FROM votes v
WHERE v.user_id = u.user_id) as user_total_votes_done
FROM users as u
ORDER BY u.total_points DESC
LIMIT 10

Multiple tables, multiple statement

Let's take 3 tables that has all tons of rows:
TABLE Posts
PostPID
PostUID
PostText
TABLE Users
UserUID
UserName
TABLE Favorites
FavoriteUID
FavoritePID
Now, in order to get all the recent posts I perform a query such as:
SELECT p.PostPID, p.PostUID, p.PostText, u.UserUID, u.UserName
FROM Posts AS p
JOIN Users AS u
ON p.PostUID = u.UserUID
ORDER BY p.PostPID DESC
LIMIT 0, 30
Which works fine. Now I was wondering, how could I get only the posts a certain UserUID prefers? So only the one with FavoriteUID = UserUID = X?
You could use a subquery.
...
Where p.PostUID in (select f.FavoritePID from Favorite f where f.FavoriteUID = UserUID)
...
second join will do the same
SELECT
p.PostPID, p.PostUID, p.PostText, u.UserUID, u.UserName
FROM
Posts AS p
JOIN
Users AS u ON p.PostUID = u.UserUID
Join
Favorites as f on f.FavoriteUID = u.UserUID and f.FavoritePID=p.PostPID
ORDER
BY p.PostPID DESC
LIMIT 0, 30

Categories