I have two tables:
items:
| item_id | title |
comments:
| comment_id | item_id | posted_at | author_id | text |
Where posted_at is the time a comment was posted.
How can I get a list of all items, with the time each of them was last commented on and the author_id of that last comment?
For this, you don't necessarily need the 'items' table if all you want are 'item_id'.
Start by writing a query that gets the latest comment time for each item_id like this:
SELECT item_id, MAX(posted_at) AS latestComment
FROM comments
GROUP BY item_id;
Now, you can join that with your comments table on the condition that the item_id and latestComment columns match to get the latest comment author for each item:
SELECT c.item_id, c.author_id, c.posted_at
FROM comments c
JOIN(
SELECT item_id, MAX(posted_at) AS latestComment
FROM comments
GROUP BY item_id) temp ON temp.item_id = c.item_id AND temp.latestComment = c.posted_at;
If you do need any information form the items table, you can just join the above query to the items table using the item_id column to get what you need.
EDIT
If you want to add requirements for items you can join the above table, and put them in either the WHERE clause or even the ON statement of your join, like this:
SELECT c.item_id, c.author_id, c.posted_at
FROM comments c
JOIN items i ON i.item_id = c.item_id AND i.title LIKE '%Apple%'
JOIN(
SELECT item_id, MAX(posted_at) AS latestComment
FROM comments
GROUP BY item_id) temp ON temp.item_id = c.item_id AND temp.latestComment = c.posted_at;
I just made up an example requirement. This query should pull the latest comment for all items that have a title containing the word 'Apple'. Note that this is an inner join, so you will only see items that do have comments. If you want to see all items, I recommend an outer join.
You need the most recent comment for each item. There are three parts to that.
First: most recent
SELECT MAX(comment_id) FROM comments GROUP BY item_id
Second: most recent comment
SELECT comments.author_id, comments.posted_at
FROM comments
WHERE comments.comment_id IN
(SELECT MAX(comment_id) FROM comments GROUP BY item_id)
Third. Most recent comment for each item.
SELECT items.item_id, items.title, comments.author_id, comments.posted_at
FROM items
LEFT JOIN comments
ON items.item_id = comments.item_id
AND comments.comment_id IN
(SELECT MAX(comment_id) FROM comments GROUP BY item_id)
The trick here is to find the single most recent comment for each item, and then use it. The left join operation preserves those items that have no comments. This query uses comment_id as a proxy to search for the latest posted_at. It assumes comment_id is an autoincrement column, and that later comments have higher comment_id values than earlier comments.
A compound index on the comments table on (item_id, comment_id) will help performance here, by accelerating the GROUP BY subquery.
You can try using max(posted_at) group by item_id and join it with you comments table on these 2 columns.
What about;
Select title,author_id,MAX(posted_at) as LastTime From items i
join comments c
on c.item_id = i.item_id
group by title,author_id
This should give you what you are looking for if the same post exists multiple times. If not, you could even remove the MAX and add other columns.
Related
I have one table products with id of product and name.
Second table is products_last_usage where I keep product_id, user_id and last_used_at.
Whenever a user clicks on a product, I have the field last_used_at updated.
Now, I need a query to list all products, and order them first by last_used_at, and then by name of product. It has to be PER USER. i.e. every user will have his own order of the table.
But I need all products, even if there are no records of them in the second table.
How to do that?
You can help me with a rails query or mysql query.
You can use a left join:
select p.*
from products p left join
products_last_usage plu
on plu.product_id = p.id and plu.user_id = $user_id
order by (last_used_at is not null) desc, last_used_at desc;
Ordering by last_used_at desc should also work. However, I think it is clearer to explicitly handle NULL values.
I think you can start from something like this. Pls next time post sample data, expected results etc.
SELECT A.PRODUCT_ID
, A.PRODUCT_NAME
, B.USER_ID
, B.LAST_USED_AT
FROM PRODUCTS A
LEFT JOIN PRODUCTS_LAST_USAGE B ON A.PRODUCT_ID = B.PRODUCT_ID
ORDER BY B.USER_ID, B.LAST_USED_AT DESC, A.PRODUCT_NAME;
Sorry for the poor question title, I'm not sure of the correct terminology to describe what I'm asking.
I have two tables:
posts:
p_id | p_author | p_text
post_comments:
pc_id | pc_p_id | pc_author | pc_text
The pc_p_id for each comment corresponds to the p_id of the post.
I want to select:
The p_id and p_text for all of the posts from a specific author
The number of comments for the corresponding post
I can do the first part with a query like this (supposing "1" is an author):
SELECT p_id, p_text FROM posts WHERE p_author = 1
And I can get the number of comments for a specific post like this (supposing "12" is a post id):
SELECT COUNT(*) FROM post_comments WHERE pc_p_id = 12
My question is, how can I combine the two queries so that I get the p_id and p_text for all of the posts from a specific author, along with the number of comments for the corresponding posts?
I tried using a LEFT JOIN like the following, but it gives me a syntax error:
SELECT t1.p_id, t1.p_text, t2.COUNT(*)
FROM posts t1 LEFT JOIN post_comments t2
WHERE t1.p_author = 1 AND t2.pc_p_id = t1.p_id
ORDER BY t1.p_id DESC
Here is the SQL Fiddle:
http://sqlfiddle.com/#!2/e9b975
SELECT p_id, p_text, count(1)
FROM posts p
JOIN post_comments pc
ON p.p_id = pc.pc_p_id
WHERE p_author = 1
GROUP BY p_id, p_text
Here is the edited version that does an embedded select:
SELECT p_id, p_text,
(SELECT COUNT(1) FROM post_comments WHERE pc_p_id = p.p_id) AS count
FROM posts p
WHERE p_author = 1
I verified this in your fiddle
LEFT_JOIN is used when you want all rows from the left-hand table, but there may not be corresponding rows in the right-hand table. Otherwise you want a straight (inner) join. Also, when using an aggregation function like COUNT() you have to use a GROUP_BY clause if you have any non-aggregate columns selected.
SELECT p.p_id, p.p_text, count( pc.pc_id ) AS CountFROM posts p, post_comments pcWHERE pc.pc_p_id = p.p_idAND p.p_author =1AND pc.pc_p_id =12
I have three tables that are joined. I almost have the solution but there seems to be one small problem going on here. Here is statement:
SELECT items.item,
COUNT(ratings.item_id) AS total,
COUNT(comments.item_id) AS comments,
AVG(ratings.rating) AS rate
FROM `items`
LEFT JOIN ratings ON (ratings.item_id = items.items_id)
LEFT JOIN comments ON (comments.item_id = items.items_id)
WHERE items.cat_id = '{$cat_id}' AND items.spam < 5
GROUP BY items_id ORDER BY TRIM(LEADING 'The ' FROM items.item) ASC;");
I have a table called items, each item has an id called items_id (notice it's plural). I have a table of individual user comments for each item, and one for ratings for each item. (The last two have a corresponding column called 'item_id').
I simply want to count comments and ratings total (per item) separately. With the way my SQL statement is above, they are a total.
note, total is the total of ratings. It's a bad naming scheme I need to fix!
UPDATE: 'total' seems to count ok, but when I add a comment to 'comments' table, the COUNT function affects both 'comments' and 'total' and seems to equal the combined output.
Problem is you're counting results of all 3 tables joined. Try:
SELECT i.item,
r.ratetotal AS total,
c.commtotal AS comments,
r.rateav AS rate
FROM items AS i
LEFT JOIN
(SELECT item_id,
COUNT(item_id) AS ratetotal,
AVG(rating) AS rateav
FROM ratings GROUP BY item_id) AS r
ON r.item_id = i.items_id
LEFT JOIN
(SELECT item_id,
COUNT(item_id) AS commtotal
FROM comments GROUP BY item_id) AS c
ON c.item_id = i.items_id
WHERE i.cat_id = '{$cat_id}' AND i.spam < 5
ORDER BY TRIM(LEADING 'The ' FROM i.item) ASC;");
In this query, we make the subqueries do the counting properly, then send that value to the main query and filter the results.
I'm guessing this is a cardinality issue. Try COUNT(distinct comments.item_id)
My database has news articles and blog posts. The primary key for both is an ItemID that is unique across both tables.
The articles are in a table that has the following fields
item_id
title
body
date_posted
The blogposts table has the following fields
item_id
title
body
date_posted
both tables have extra fields unique to them.
I have a third table that holds meta information about articles and posts.
The items table has the following fields
item_id
source_id
...
every blogpost and article has a record in the items table and a record in its respective table.
What I am trying to do is build a query that will count the number of items posted per day. I can do it for one table using a count grouped by date_posted but how to combine articles and posts count in one query?
Similar to Dems, but slightly simpler:
select date_posted, count(*)
from (select date_posted from article union all
select date_posted from blogposts) v
group by date_posted
You can do it two ways.
1. Join everything together and then aggregate (See Tom H's answer).
2. Aggregate each table, UNION them, and aggregate again.
Option 1 may seem shorter, but will mean that you may not benefit from INDEXes on the root tables (As they have to be re-ordered for the JOIN). So I'll show option 2, which is the direction you were headed any way.
SELECT
date_posted,
SUM(daily_count) AS daily_count
FROM
(
SELECT date_posted, COUNT(*) AS daily_count FROM article GROUP BY date_posted
UNION ALL
SELECT date_posted, COUNT(*) AS daily_count FROM blogposts GROUP BY date_posted
)
AS combined
GROUP BY
date_posted
This should be fastest, provided that you have an index on each table where date_posted is the first field in the index. Other-wise the tables will still need to be re-ordered for the aggregation.
I would have used a different table design for this, with types and subtypes. Your Items table has a single column primary key and your Blog_Posts and Articles tables' primary keys are the same ID with a foreign key to the Items table. That would make something like this pretty easy to do and also helps to ensure data integrity.
With your existing design, your best bet is probably something like this:
SELECT
I.item_id,
I.source_id,
COALESCE(A.date_posted, B.date_posted) AS date_posted,
COUNT(*) AS date_count
FROM
Items I
LEFT OUTER JOIN Articles A ON
A.item_id = I.item_id AND
I.source_id = 'A' -- Or whatever the Articles ID is
LEFT OUTER JOIN Blog_Posts B ON
B.item_id = I.item_id AND
I.source_id = 'B' -- Or whatever the Blog_Posts ID is
GROUP BY
I.item_id,
I.source_id,
COALESCE(A.date_posted, B.date_posted)
You could also try using a UNION:
SELECT
SQ.item_id,
SQ.source_id,
SQ.date_posted,
COUNT(*) AS date_count
FROM
(
SELECT I1.item_id, I1.source_id, A.date_posted
FROM Items I1
INNER JOIN Articles A ON A.item_id = I1.item_id
WHERE I1.source_id = 'A'
UNION ALL
SELECT I2.item_id, I2.source_id, B.date_posted
FROM Items I2
INNER JOIN Articles B ON B.item_id = I2.item_id
WHERE I2.source_id = 'B'
)
select item_id, date_posted from blogposts where /* some conditions */
union all select item_id, date_posted from articles where /* some conditions */
You'll probably need to put that into a subquery, and if you so desire, join it with other tables, when running the group by. But the main point is that union is the operator you use to combine like data from different tables. union all tells the database that you don't need it to combine duplicate records, since you know that the two tables will never share an item_id, so it's a little faster (probably).
I have 2 tables authors and authors_sales
The table authors_sales is updated each hour so is huge.
What I need is to create a ranking, for that I need to join both tables (authors has all the author data while authors_sales has just sales numbers)
How can I create a final table with the ranking of authors ordering it by sales?
The common key is the: authorId
I tried with LEFT JOIN but I must be doing something wrong because I get all the authors_sales table, not just the last.
Any tip in the right direction much appreciated
If you're looking for aggregate data of the sales, you'd want to join the tables, group by the authorId. Something like...
select authors.author_id, SUM(author_sales.sale_amt) as total_sales
from authors
inner join author_sales on author_sales.author_id = authors.author_id
group by authors.author_id
order by total_sales desc
However (I couldn't distinguish from your question whether the above scenario or next is true), if you're only looking for the max value of the author_sales table (if the data in this table is already aggregated), you can join on a nested query for author_sales, such as...
select author.author_id, t.sales from authors
inner join
(select top 1 author_sales.author_id,
author_sales.sale_amt,
author_sales.some_identifier
from author_sales order by some_identifier desc) t
on t.author_id = author.author_id
order by t.sales desc
The some_identifier would be how you determine which record is the most recent for author_sales, whether it is a timestamp of when it was inserted or an incremental primary key, however it is set up. Depending on if the data in author_sales is aggregated already, one of these two should do it for you...
select a.*, sum(b.sales)
from authors as a
inner join authors_sales as b
using authorId
group by b.authorId
order by sum(b.sales) desc;
/* assuming column sales = total for each row in authors_sales */