SELECT u.*,(
SELECT COUNT(*) FROM (
SELECT id,author_id FROM posts p
UNION ALL
SELECT id, author_id FROM updates up
) total_table WHERE author_id = u.id) as total_post FROM users u ORDER BY total_post
i have query line want to DB::raw. Can you help me??? many thanks
Use raw query like:
DB::select(DB::raw('SELECT u.*,(
SELECT COUNT(*) FROM (
SELECT id,author_id FROM posts p
UNION ALL
SELECT id, author_id FROM updates up
) total_table WHERE author_id = u.id) as total_post FROM users u ORDER BY total_post'));
Reference
Or you can do this by using DB::satement
$query='SELECT u.*,(SELECT COUNT(*) FROM (
SELECT id,author_id FROM posts p
UNION ALL
SELECT id, author_id FROM updates up) total_table WHERE author_id = u.id) as total_post FROM users u ORDER BY total_post';
$data=DB::statement($query);
Like this.
Related
I have 2 tables, articles and article_comments. Trying to get the comment count on the article listing page. However, any articles which do not have any comment under it is not being returned by my query.
SELECT `articles`.*, COUNT(comments.id) AS comment_count
FROM `articles` as `articles`
LEFT JOIN `article_comments` as `comments` ON `articles`.`id` = `comments`.`article_id`
How can I make the query return all rows from articles with comment_count as 0 if there are no comments for that post?
Obviously your query is missing a GROUP BY clause, that should enumerate all the columns from the articles table - or, if you are running MySQL with sql mode ONLY_FULL_GROUP_BY disabled, it shoul contain the primary key of articles.
It might be simpler to express this with a correlated subquery:
select
a.*,
coalesce(
(select count(*) from article_comments ac where ac.article_id = a.id),
0
) comments_count
from articles a
You can also pre-aggregate in a subquery:
select
a.*,
coalesce(c.comments_count, 0) comments_count
from articles a
left join (
select article_id, count(*) comments_count
from article_comments
group by article_id
) c on c.article_id = a.id
Table -1 : Comment id, comment,user_id,comment Date
Table -2: Users id, user_name, full_name, password
now i want to get user detaiils records who is last comment
like
query is :
select c.*,
(select user_name
from users
where id = c.user_id
) as user_name,
(select full_name
from users
where id = c.user_id
) as full_name
from comment as c, users as u
group by c.user_id
order by comment_date DESC
SELECT Users.*,
Comment.*
FROM Users
INNER JOIN Comment ON (Comment.user_id = Users.id)
GROUP BY Users.id
ORDER BY Comment.id DESC
that should work
Here is your query
select users.* from users inner join comments on
users.user_id = comments.user_id
order by comments.comment_date desc limit 1
Another way to do this
select * from users where user_id =
(select user_id from comments order by comment_date desc limit 1)
I am working on an sql query where I have three tables posts, users and comments. I want to display all posts with its users and number of comments on this. I have following query but it is giving me wrong result:
SELECT
c.userid, count(c.userid), p.postid
FROM comments c, posts p
where c.userid = p.userid group by c.userid
In addition to above query I also require firstname and lastname from users table.
Try something like this,
SELECT
u.userid, u.firstname, u.lastname,p.post, p.postid,
count(c.userid) totalcoments -- may be c.commentid
FROM users u
JOIN posts p ON p.userid=u.userid
LEFT JOIN comments c OM c.postid=p.postid
GROUP BY u.userid, u.firstname, u.lastname,p.post, p.postid
Try something like the following:
SELECT
postid
, p.userid
, COALESCE((
SELECT COUNT( * ) FROM comments WHERE postid = p.id
), 0 ) AS cnt_postid
, COALESCE( ( SELECT CONCAT( firstname, ' ', lastname ) FROM users WHERE userid = p.userid ), 'N/A' ) AS NAME
FROM posts p
LEFT JOIN comments c ON c.postid = p.id
GROUP BY postid
ORDER BY postid
you are probably getting the same amount of count because you not using a group by.
GROUP BY must always be used when using aggregate function. What the group by does is that it will select all unique posts and the the count will return the number of users for that one unique post
I`m trying to extract the activity of a user with a single query. I need to do it with one single query so I can use zend paginator.
I have 5 tables: users, replies, threads, wiki_articles and wiki_article_revisions. Each table has 2 common columns created_by and created_on.
I've tried using left join but I think what it returns is not correct and I'm unable to order all activity by created_on
Here is the join I've tried:
SELECT * FROM `users` u
LEFT join `replies` r ON u.id = r.created_by
LEFT join `threads` t ON u.id = t.created_by
LEFT join `wiki_articles` wa ON u.id = wa.created_by
LEFT join `wiki_article_revisions` war ON u.id = war.created_by
WHERE (u.`name` = 'CGeorges')
My thought process was wrong. I should have used UNION for this, like:
(SELECT id, name, created_on FROM users WHERE id = 1)
UNION
(SELECT id, name, created_on FROM threads WHERE created_by = 1)
UNION
(SELECT id, content, created_on FROM replies WHERE created_by = 1)
UNION
(SELECT id, title, created_on FROM wiki_articles WHERE created_by = 1)
ORDER by created_on DESC
I have tables tbl_posts and tbl_comments with primary keys post_id and comment_id respectively. I tried this code:
$allPosts=mysql_query("
select c.comment_id, post_id, count(*)
from post u,
comments c
where u.comment_id = c.comment_id
group by comment_id, post_id
LIMIT 10
");
but I have no clue what it does. How do I combine two tables so that the total comments determines the order of the listed posts from tbl_posts?
Try this, it's more readable if you separate per lines and work with joins
select c.comment_id, post_id, count(*)
from post u join comments c
on u.comment_id = c.comment_id
group by comment_id, post_id LIMIT 10
It looks like you have tables named tbl_comment and tbl_post but your query has them listed as just comment and post.
select c.comment_id, post_id, count(*)
from tbl_post u, tbl_comments c
where u.comment_id = c.comment_id
group by comment_id, post_id LIMIT 10
$allPosts=mysql_query("select c.comment_id, post_id, count(*) from tbl_post u, tbl_comments c where u.comment_id = c.comment_id group by comment_id, post_id LIMIT 10");
This just fixes the query so it runs, and does not address any content issues you may have, namely the group by on both (what I am guessing) are primary keys.
** EDIT **
To fix the sorting try:
SELECT tbl_post.comment_id, count(*)
FROM tbl_post, tbl_comments
WHERE tbl_post.comment_id = tbl_comment.comment_id
GROUP BY comment_id LIMIT 10
ORDER BY count(*)
Explanation of your SQL:
You are selecting column comment_id from table comments, column post_id from table post using a inner join, grouping by comment_id, post_id, with a limit of 10 results.
I would try:
$allPosts = mysql_query("SELECT * FROM
(SELECT c.comment_id, u.post_id, COUNT(*) AS 'count' FROM post u
LEFT JOIN comments c ON c.comment_id = u.comment_id
GROUP BY c.comment_id, u.post_id)
ORDER BY count DESC LIMIT 10");