Count multiple tables - mysql

I have several mysql tables like this:
blogs
entry_id
member_d
articles
entry_id
member_d
posts
entry_id
member_d
I want to count the total entries by a specific member. I currently have this (while only using 3 tables for this example, there is in fact about 10 - 20 tables all structured the same):
SELECT COUNT('member_id') FROM blogs WHERE member_id=3 LIMIT 1;
SELECT COUNT('member_id') FROM articles WHERE member_id=3 LIMIT 1;
SELECT COUNT('member_id') FROM posts WHERE member_id=3 LIMIT 1;
You see the repetition? Is there any way of condensing that down to 1 query for example (doubt this works):
SELECT COUNT(blogs.'member_id') as total_blogs,
COUNT(articles.'member_id') as total_articles,
COUNT(posts.'member_id') as total_posts
FROM blogs,articles,posts WHERE member_id=3 LIMIT 1;
P.S. Tried searching stackoverflow and google but keep getting things about using COUNT(*) or using groups, etc...

This Works,
SELECT
(SELECT COUNT('member_id') FROM blogs WHERE member_id=3) as total_blogs,
(SELECT COUNT('member_id') FROM articles WHERE member_id=3) as total_articles,
(SELECT COUNT('member_id') FROM posts WHERE member_id=3) as total_posts
and gives you all info in only one record

SELECT COUNT(*) FROM(
SELECT member_id FROM blogs
UNION ALL
SELECT member_id FROM articles
UNION ALL
SELECT member_id FROM posts
) AS activity
WHERE member_id=3
GROUP BY member_id
Sqlfiddle demonstration: http://sqlfiddle.com/#!2/366bd/2

Just for the record, I add here a second solution that admit multiple selection of IDs in a single query
SELECT m.member_id,
COALESCE(blogs.total_blogs,0) as total_blogs,
COALESCE(articles.total_articles,0) as total_articles,
COALESCE(posts.total_posts,0) as total_posts
FROM members m -- I guess this table exists
LEFT JOIN (SELECT member_id, COUNT('member_id') as total_blogs FROM blogs GROUP BY member_id) as blogs on m.member_id = blogs.member_id
LEFT JOIN (SELECT member_id, COUNT('member_id') as total_articles FROM articles GROUP BY member_id) as articles on m.member_id = articles.member_id
LEFT JOIN (SELECT member_id, COUNT('member_id') as total_posts FROM posts GROUP BY member_id) as posts on m.member_id = posts.member_id
where m.member_id in (3,4,5)
fiddle here

Related

Count in mysql database

This my query:
Select articles.id,articles.userid,articles.article,count(articles_likes.id), count(article_dislikes.id)
from articles
Left join article_likes
on article_likes.id=articles.id
Left join article_dislikes
on article_dislikes.id=articles.id
group by articles.id ;
I want to count the number of rows in article_likes table and article_dislikes table im getting the value correct upto 2 rows..when there is a third and so on entries..I'm getting wrong counting of rows...
I don't know where the problem is ..I think I may be getting wrong values because I'm using the same table for two times...please help me
I have three tables
1)articles contains id,userid and article
2)Articles_likes table contains-like_id ,user_id and article_id
3)Articles_dislikes table contains dislike_id ,user_id and article_id
Aggregate separately in each of the tables articles_likes and articles_dislikes and then join articles to the results:
select a.id, a.userid, a.article,
coalesce(l.likes, 0) likes,
coalesce(d.dislikes, 0) dislikes
from articles a
left join (
select article_id, count(*) likes,
from articles_likes
group by article_id
) l on l.article_id = a.id
left join (
select article_id, count(*) dislikes,
from articles_dislikes
group by article_id
) d on d.article_id = a.id
Also the correct join of articles_likes and articles_dislikes to articles is by the column article_id to the id of articles.

How show "repost" like retweets in mysql query?

I'm working on a project where I have posts that can be reposted to the pure style of twitter (retweets). The posts are saved in a table "posts" (id, userid, title, content, date) and the reposts are saved in another table "reposts" (postid, userid, date). I need to show posts and reposts of a user arranged chronologically and in a descending order.
So far what I have been able to do is show both tables but the reposts are shown ordered with the creation date of the post, not with the date of the repost. If I repost a post from 2 years ago, I want the post to be shown now (reposts date), and not ordered with the posts from 2 years ago (posts date).
This is my query:
SELECT * FROM (SELECT p.* FROM post AS p WHERE userid='$id' UNION SELECT p.* FROM post AS p WHERE p.id IN (SELECT postid FROM repost WHERE userid='$id')) a ORDER BY date DESC
Join reposts and posts and pick the columns you want from each table.
SELECT *
FROM (SELECT p1.postid,
p1.userid,
p1.title,
p1.content,
p1.date
FROM posts p1
WHERE p1.userid = ?
UNION ALL
SELECT r1.postid,
r1.userid,
p2.title,
p2.content,
r1.date
FROM reposts r1
INNER JOIN posts p2
ON p2.postid = r1.postid
WHERE r1.userid = ?) x
ORDER BY x.date DESC;

Using the result of a sub-select in the WHERE clause in a MySQL query

SELECT
*, (SELECT SUM(rating) FROM votes WHERE votes.postId = posts.id) AS rating
FROM posts
WHERE rating > 10
There are multiple entries in my table where the sum of the ratings in votes with the corresponding post ID is greater than 10, but this query is not returning any results. Why?
Here is the relevant portion of my database structure:
TABLE posts
- id
TABLE votes
- postId
- rating
Any help would be greatly appreciated.
You need to name the subquery column, like this
SELECT * FROM
(SELECT sum(rating) as rating_sum, * FROM posts) AS rating
INNER JOIN posts on posts.id = rating.id
WHERE rating.rating_sum>10
try this.
SELECT posts.*, (select SUM(rating) as totalRating
from votes
where votes.postid = posts.id) as totalRating
FROM posts
WHERE totalRating > 10

MySQL query already GROUPed and ORDERed : how to ORDER inside the GROUPs?

I have this query:
SELECT id_user, COUNT(*) as count
FROM posts
GROUP BY id_user
ORDER BY COUNT(*) DESC
which gives me the id_user ordered by occurrences, and the number of each occurrence.
Can I get, in the same request, the LAST post from each 'id_user'? i.e. I want to select the last 'post' too, but when I do
SELECT id_user, post, COUNT(*) as count
Tthe value in 'post' isn't the last one (nor the first one; actually I don't know how groups are ordered). Should I run another query?
I believe u can accomplish this by adding max(post_id) last_post to your select.
This ought to do it in one query:
SELECT
p.id_user,
ap.post AS last_post,
COUNT(*) as count
FROM
posts p
JOIN posts ap on (
p.id_user = ap.id_user
AND ap.post_id = (
SELECT MAX(post_id) FROM posts ip WHERE p.id_user = ip.id_user
)
GROUP BY
p.id_user,
ap.post
ORDER BY
COUNT(*) DESC

MySQL subqueries

Can we do this query without subqueries?
SELECT login, post_n,
(SELECT SUM(vote) FROM votes WHERE votes.post_n=posts.post_n)AS votes,
(SELECT COUNT(comments.post_n) FROM comments WHERE comments.post_n=posts.post_n)AS comments_count
FROM users, posts
WHERE posts.id=users.id AND (visibility=2 OR visibility=3)
ORDER BY date DESC LIMIT 0, 15
tables:
Users: id, login
Posts: post_n, id, visibility
Votes: post_n, vote
id — it`s user id, Users the main table.
Yeah, it's possible:
SELECT login, post_n,
SUM(vote) as votes,
FROM users
JOIN posts using(id)
LEFT JOIN votes using(post_n)
WHERE visibility=2 OR visibility=3
GROUP BY login, post_n
Then flatten the result:
select * from
(
SELECT login, post_n,
SUM(vote) as votes,
FROM users
LEFT JOIN posts using(id)
LEFT JOIN votes using(post_n)
WHERE visibility=2 OR visibility=3
GROUP BY login, post_n
) as votes_count
Then join the comments:
select votes_count.login, votes_count.post_n, votes_count.votes,
COUNT(comments.post_n) as comments_count
from
(
SELECT login, post_n,
SUM(vote) as votes,
FROM users
LEFT JOIN posts using(id)
LEFT JOIN votes using(post_n)
WHERE visibility=2 OR visibility=3
GROUP BY login, post_n
) as votes_count
LEFT JOIN comments using(post_n)
GROUP BY votes_count.login, votes_count.post_n
ORDER BY date DESC LIMIT 0, 15
you can store vote sum and post count in a 'users' table and update them via trigger or 'update' query.
i have test both variants and test variant when we using join or where to merge our tables.
No subqueries variant more slow, 0.0208 sec, and mysql use only 271 rows in votes table, but when i have use joins it use whole rows. Here no subqueries variant:
SELECT res.*, COUNT(*) FROM
(
SELECT login, posts.post_n, SUM(vote)AS votes
FROM users, posts, votes
WHERE users.id=posts.id AND posts.post_n=votes.post_n AND visibility=3
GROUP BY posts.post_n
)AS res, comments
WHERE comments.post_n=res.post_n
GROUP BY res.post_n
ORDER BY date DESC
LIMIT 0, 15
And subqueries varian performed only 0.0027 sec, it`s no cache but using indexes in all tests.
p.s. sorry for my english