Currently I'm running one query to get all posts for a user, then in the loop for that, I am querying for the latest 3 comments for that particular post. Super inefficient; I'm querying over and over again for every post.
I would like to consolidate my queries so that I query just once for all posts, and just once for all comments for those particular posts. At the moment I have a comma-separated list that I made for all posts for this user (e.g. "1,5,18,9")
posts table:
posts.id
posts.userid
comments table:
comments.id
comments.relid (this is the postid)
comments.userid
The query should use the $posts_list I have, which is the comma-separated list of posts. Or a subselect for all posts for this user, but that seems inefficient since I already have the post list in a string.
Thanks so much for any help!
Try this query -
SELECT p.id, p.userid, c.id, c.userid
FROM posts p
JOIN (
SELECT c1.*, COUNT(*) rank FROM comments c1
LEFT JOIN comments c2
ON c2.relid = c1.relid AND c2.id <= c1.id
GROUP BY c1.relid, c1.id
) c
ON p.id = c.relid
WHERE rank < 4
And add condition you need, i.e. - WHERE p.userid IN (1,5,18,9).
Related
I have a custom user role let's call it "user-role" that can create only a custom post type lets call it cpt under many categories.
What I'm trying to get is a page that displays 20 users per page(will have pagination) with the fallowing info(it should show only users that have more than 2 posts published):
1)Total number of cpt posts published by this user
2)Total number of posts under category 1 and 2 published by this user
I've tried achieving this using WordPress get_users function to fetch for all authors with "user-role" as their role and then loop throw this found users using count_many_users_posts() gotten the total number of posts by each user and then with a new WP_Query fetch all the posts having the initial author id and beeing in the specified category. with a new foreach throw this lasts posts I was counting the number of posts found per user.
Limiting the solution to 2 users the logic seems to work.
The issue: The script runs too slow, and the number or users are very big (approx 3k) so running 2 loops inside another loop is way too slow for this to work.
What I was thinking was to use a custom query where I should combine a select the users table inner joined with usermeta table for the specified user role to get only the user ids for the needed role and all this query inner joined again with posts table from which to get only publish posts with the post_type = cpt, and count the returned rows.
if possible the above joined with a query that will filter again the results accordingly to term_relationships table to count only posts with category 1,2 (this should be returned to the total posts number)
What I've tried but didn't worked (I'm getting no results back but I really don't think I'm using joins and where clause correctly) is:
SELECT $wpdb->users.id, count(*) FROM $wpdb->users
INNER JOIN $wpdb->usermeta on $wpdb->users.id = $wpdb->usermeta.user_id
WHERE $wpdb->usermeta.meta_key = 'wp_capabilities'
AND meta_value RLIKE '[[:<:]]user-role[[:>:]]'
INNER JOIN $wpdb->posts on $wpdb->users.id = $wpdb->posts.post_author
WHERE $wpdb->posts.post_type = "cpt" AND $wpdb->posts.post_status = "publish"
having count(*)>2
I don't know how close I'm with the above but this is what I could build to count all the posts of a specific role users in only one query, to this I then would like to add the count for the posts in the above categories (I can hardcode the terms ids for the categories so there is no need to get other tables in this ecuation except $wpdb->term_relationships)
so after getting the $ids and count(*) from the above I'm thinking the results should be again inner joined with $wpdb->term_relationships something like this:
INNER JOIN $wpdb->term_relationships on $wpdb->term_relationships.object_id = $wpdb->posts.id
WHERE term_taxonomy_id IN (1,2)
This is what I came up with, is there a standard solution to this? Or does the Wordpress database and WordPress functions doesn't cover this at this point for such a large set of data?
You can try below query for the count of posts for each author having user level = 10 in custom post type
SELECT COUNT(u.`ID`) AS post_count,p.`ID`,u.`user_nicename` FROM
`wp_posts` p
LEFT JOIN `wp_users` u ON( p.`post_author`=u.`ID`)
INNER JOIN `wp_usermeta` um ON (u.`ID` = um.`user_id` AND um.`meta_key`='wp_user_level')
WHERE p.`post_type`='cpt' AND p.`post_status`='publish' AND um.`meta_value`='10'
GROUP BY u.`ID`
HAVING post_count>2
This query is for count of posts by authors in a specific category
SELECT COUNT(CASE WHEN wtt.`term_id`='3' THEN u.`ID` END) AS cat1_count,
COUNT(CASE WHEN wtt.`term_id`='4' THEN u.`ID` END) AS cat2_count,
p.`ID`,u.`user_nicename` FROM
`wp_users` u
INNER JOIN `wp_usermeta` um ON (u.`ID` = um.`user_id` AND um.`meta_key`='wp_user_level')
LEFT JOIN `wp_posts` p ON(u.`ID`= p.`post_author`)
LEFT JOIN `wp_term_relationships` wtr ON (p.`ID` = wtr.`object_id`)
LEFT JOIN `wp_term_taxonomy` wtt ON (wtr.`term_taxonomy_id`=wtt.`term_taxonomy_id`)
WHERE p.`post_type`='cpt' AND p.`post_status`='publish' AND um.`meta_value`='10'
GROUP BY u.`ID`
For dummy category no.s i have use termid 3 and 4 to take count you need to add proper termids in order to count the posts of authors in specified categories
I have created a simple blog in php/mysql that show in homepage latest 100 posts and for every post show the comments's number.
This is the pseudocode:
Mysql query to get latest 100 posts.
While cicle:
Get title and body of each post.
Mysql query to get the comments's number of the post.
Database structure:
Post:
-id
-title
-body
-date
Comments:
-id
-id_post
-id_user
-body
-date
Is there a way to avoid 100 queries ?
It's a fairly straightforward query:
SELECT p.id, p.title, p.body. p.date, COUNT(c.id) AS comment_count
FROM Post p
LEFT JOIN Comments c ON p.id=c.id_post
GROUP BY p.id, p.title, p.body. p.date
ORDER BY p.id DESC
LIMIT 100
(Please note it's untested, take it as a starting point.)
I know it's a common believe that a database is nothing but a fancy file system and SELECT * FROM data is all the SQL you'll ever need to know but investing some time in learning basic SQL it's absolutely worth the effort.
You can write a sql query that JOINS both of your tables together, to return all of the information you want:
SELECT
p.id,
p.title,
p.body,
p.date,
COUNT(c.id)
FROM Post p
LEFT JOIN Comments c ON p.id = C.post_id
GROUP BY p.id, p.title, p.body, p.date
ORDER BY p.date DESC
LIMIT 100
Jeff Atwood has a great visual guide explaining how joins work.
We are effectively selecting the top 100 posts in date order, counting the total comments (if they exist - that's why we use a LEFT JOIN as otherwise, if we use an INNER JOIN, we will ONLY return posts that have comments).
We GROUP BY as that's how COUNT (and other aggregation functions like SUM and AVG work - we need to tell them what the rule is for counting/summing/averaging our rows.
We ORDER BY the post date, in DESCending order (latest first) to ensure we return posts in the order they were made.
The LIMIT 100 statement only returns the first 100 rows in MySql. Change it to any number you wish (a top 10, 50 etc) if you want to vary the number of posts in the summary.
The overall result is that you now have a result set that contains the post information, and the number of comments, all in one query. You can then display these results in your web application any way you see fit.
Just join your tables and then count the number of comments for that post.
SELECT p.id, p.title, p.body, COUNT(pc.id) as comments
FROM Post p
LEFT JOIN Comments pc ON (p.id = pc.id_post)
GROUP BY pc.id_post LIMIT 100
Hope that is what you are after.
Join your posts table with your posts-info tables, and select them at the same time
select p.id,pi.comments from posts as p, post_info as pi where p.id == pi.id
or something like that.
EDIT:
select p.title,p.body,p.date,count(c.id) from post as p, comments as c where p.id == c.id_post
I believe.
i have the following query that pulls posts that belong to a blog
SELECT p.*
FROM `Posts` p
INNER JOIN Blogs b ON b.id = p.blog_id
Is there a MySQL function that does the opposite of "ON"? If i wanted to query posts that didn't belong to a blog in the "Blogs" database table? For instance if the blog was deleted.
You need to use either LEFT JOIN - NULL combo, like this:
SELECT p.*
FROM Posts p
LEFT OUTER JOIN Blogs b
ON b.id = p.blog_id
WHERE b.id IS NULL
... or NOT IN dependent query (with so-called 'anti-join'):
SELECT *
FROM Posts
WHERE blog_id NOT IN (SELECT id FROM Blogs)
I admit I almost exclusively use the first form, but actually they have the similar performance: I'd recommend at least glancing through this article with detailed explanation of similarities and differences between these queries; it's quite an enlightening reading. )
It's all about the JOIN.
SELECT p.*
FROM `Posts` p
LEFT OUTER JOIN Blogs b ON b.id = p.blog_id
WHERE b.Id IS NULL
Should do the trick for you.
Jeff Atwood has a great overview of joins
In this instance, what the LEFT OUTER JOIN does is match all rows in Blogs with all rows in Posts with the same blog_id-Id combo AND all rows in Posts that have no matching Id in Blogs. In this instance, those rows are NULL for the entries in Blogs (as there aren't any!), which is why we filter on b.Id IS NULL
There are some other ways you can achieve this too; for example:
SELECT * FROM Posts WHERE blog_id NOT IN (SELECT Id FROM Blogs)
Effectively, this queries all rows in posts that have a blog_id that isn't in the blog table.
I know this question has been asked multiple times (however, I could still not find a solution):
PHP MYSQL showing posts with comments
mysql query - blog posts and comments with limit
mysql structure for posts and comments
...
Basic question: having tables posts, comments, user... can you with one single select statement select and show all posts and all comments (with comment.user, comment.text, comment.timestamp)? How would such a select statement look like? If not, what is the easiest solution?
I also tried to JOIN the comments table with the posts table and use GROUP BY, but I got either only one comment in each row or each comment but also those posts multiple times!?
I tried the solution of the first link (nested mysql_query and then fetch) as well as the second link (with arrays). However, the first caused a bunch of errors (the syntax in that post seems to be not correct and I could not figure out how to solve it) and in the second I had problems with the arrays.
My query looks like this till now:
SELECT p.id, p.title, p.text, u.username, c.country_name, (SELECT SUM(vote_type) FROM votes v WHERE v.post_id = p.id) AS sum_vote_type FROM posts p LEFT JOIN user u ON ( p.user_id = u.id ) LEFT JOIN countries c ON ( c.country_id = u.country_id ) ORDER BY $orderby DESC
I was wondering if this issue was not very common, having posts and comments to show...?
Thank you for every help in advance!
Not knowing your database structure, it should look something like this. Note that you should replace the * characters with more explicit lists of columns you actually need.
SELECT p.*, c.*, u.* FROM posts p
LEFT JOIN comments c ON c.post_id = p.id
LEFT JOIN users u ON u.id = p.author_id
Note that if you're just trying to get counts, sums and things like that it's a good idea to cache some of that information. For instance, you may want to cache the comment count in the post table instead of counting them every query. Only count and update the comment count when adding/removing a comment.
EDIT:
Realized that you also wanted to attach user data to each comment. You can JOIN the same table more than once but it gets ugly. This could turn into a really expensive query. I also am including an example of how to alias columns so it's less confusing:
SELECT p.*, c.*, u.name as post_author, u2.name as comment_author FROM posts p
LEFT JOIN comments c ON c.post_id = p.id
LEFT JOIN users u ON u.id = p.author_id
LEFT JOIN users u2 ON u2.id = c.author_id
I'm working on building a forum with kohana. I know there is already good, free, forum software out there, but it's for a family site, so I thought I'd use it as a learning experience. I'm also not using the ORM that is built into Kohana, as I would like to learn more about SQL in the process of building the forum.
For my forum I have 4 main tables:
USERS
TOPICS
POSTS
COMMENTS
TOPICS table: id (auto incremented), topic row.
USERS table: username, email, first and last name and a few other non related rows
POSTS table: id (auto incremented), post-title, post-body, topic-id, user-id, post-date, updated-date, updated-by(which will contain the user-id of the person who made the most recent comment)
COMMENTS table: id (auto incremented), post-id, user-id and comment
On the main forum page I would like to have:
a list of all of the topics
the number of posts for each topic
the last updated post, and who updated it
the most recently updated topic to be on top, most likely an "ORDER BY updated-date"
Here is the query I have so far:
SELECT topics.id AS topic-id,
topics.topic,
post-user.id AS user-id,
CONCAT_WS(' ', post-user.first-name, post-user.last-name) AS name,
recent-post.id AS post-id,
post-num.post-total,
recent-post.title AS post-title,
recent-post.update_date AS updated-date,
recent-post.updated-by AS updated-by
FROM topics
JOIN (SELECT posts.topic-id,
COUNT(*) AS post-total
FROM POSTS
WHERE posts.topic-id = topic-id
GROUP BY posts.topic-id) AS post-num ON topics.id = post-num.topic-id
JOIN (SELECT posts.*
FROM posts
ORDER BY posts.update-date DESC) AS recent-post ON topics.id = recent-post.topic-id
JOIN (SELECT users.*,
posts.user-id
FROM users, posts
WHERE posts.user-id = users.id) as post-user ON recent-post.user_id = post-user.id
GROUP BY topics.id
This query almost works as it will get all of information for topics that have posts. But it doesn't return the topics that don't have any posts.
I'm sure that the query is inefficient and wrong since it makes two sub-selects to the posts table, but it was the only way I could get to the point I'm at.
Dash is not a valid character in SQL identifiers, but you can use "_" instead.
You don't necessarily have to get everything from a single SQL query. In fact, trying to do so makes it harder to code, and also sometimes makes it harder for the SQL optimizer to execute.
It makes no sense to use ORDER BY in a subquery.
Name your primary key columns topic_id, user_id, and so on (instead of "id" in every table), and you won't have to alias them in the select-list.
Here's how I would solve this:
First get the most recent post per topic, with associated user information:
SELECT t.topic_id, t.topic,
u.user_id, CONCAT_WS(' ', u.first_name, u.last_name) AS full_name,
p.post_id, p.title, p.update_date, p.updated_by
FROM topics t
INNER JOIN
(posts p INNER JOIN users u ON (p.updated_by = u.user_id))
ON (t.topic_id = p.topic_id)
LEFT OUTER JOIN posts p2
ON (p.topic_id = p2.topic_id AND p.update_date < p2.update_date)
WHERE p2.post_id IS NULL;
Then get the counts of posts per topic in a separate, simpler query.
SELECT t.topic_id, COUNT(*) AS post_total
FROM topics t LEFT OUTER JOIN posts p USING (topic_id)
GROUP BY t.topic_id;
Merge the two data sets in your application.
to ensure you get results for topics without posts, you'll need to use LEFT JOIN instead of JOIN for the first join between topics and the next table. LEFT JOIN means "always return a result set row for every row in the left table, even if there's no match with the right table."
Gotta go now, but I'll try to look at the efficiency issues later.
This is a very complicated query. You should note that JOIN statements will limit your topics to those that have posts. If a topic does not have a post, a JOIN statement will filter it out.
Try the following query.
SELECT *
FROM
(
SELECT T.Topic,
COUNT(AllTopicPosts.ID) NumberOfPosts,
MAX(IFNULL(MostRecentPost.Post-Title, '') MostRecentPostTitle,
MAX(IFNULL(MostRecentPostUser.UserName, '') MostRecentPostUser
MAX(IFNULL(MostRecentPost.Updated_Date, '') MostRecentPostDate
FROM TOPICS
LEFT JOIN POSTS AllTopicPosts ON AllTopicPosts.Topic_Id = TOPICS.ID
LEFT JOIN
(
SELECT *
FROM Posts P
WHERE P.Topic_id = TOPICS.id
ORDER BY P.Updated_Date DESC
LIMIT 1
) MostRecentPost ON MostRecentPost.Topic_Id = TOPICS.ID
LEFT JOIN USERS MostRecentPostUser ON MostRecentPostUser.ID = MostRecentPost.User_Id
GROUP BY T.Topic
)
ORDER BY MostRecentPostDate DESC
I'd use a left join inside a subquery to pull back the correct topic, and then you can do a little legwork outside of that to get some of the user info.
select
s.topic_id,
s.topic,
u.user_id as last_updated_by_id,
u.user_name as last_updated_by,
s.last_post,
s.post_count
from
(
select
t.id as topic_id,
t.topic,
t.user_id as orig_poster,
max(coalesce(p.post_date, t.post_date)) as last_post,
count(*) as post_count --would be p.post_id if you don't want to count the topic
from
topics t
left join posts p on
t.id = p.topic_id
group by
t.topic_id,
t.topic,
t.user_id
) s
left join posts p on
s.topic_id = p.topic_id
and s.last_post = p.post_date
and s.post_count > 1 --0 if you're using p.post_id up top
inner join users u on
u.id = coalesce(p.user_id, s.orig_poster)
order by
s.last_post desc
This query does introduce coalesce and left join, and they are very good concepts to look into. For two arguments (like used here), you can also use ifnull in MySQL, since it is functionally equivalent.
Keep in mind that that's exclusive to MySQL (if you need to port this code). Other databases have other functions for that (isnull in SQL Server, nvl in Oracle, etc., etc.). I used coalesce so that I could keep this query all ANSI-fied.