![]() |
Count Unread Message
EDITED:
I realise that what I was looking for is not practical or very easy to do. So will have to do this in two separate queries. Code:
CREATE TABLE IF NOT EXISTS `itemComments` (Then in the second I would like returned userid's of all that have made comments and the total number of comments made after the posters last message. so if userA posts a message for item1 and userB posts a message for item1 then userA would have one message to view. if userC posts another message for item1 then userA would have two messages to view and userB would have one message to view, userC would have none for item1 as they posted last. I think thats what I am after. but think this may be so complex if not impossible to query? Can anyone suggest a better method/logic to do this in. I am wanting to have a cronjob run every hour and if there are messages to view then each user will get an email to state this and a field in the users table called `commentEmailNotify` will be set to the last time() email sent. My code will then check before sending the email if they have had one already sent in the last 24 hours. no need to remind them of each message every hour! Its just that query to get the userid and the total number of messages for each item they have commented in and if there are any replies made after their last message. The total count is not really that important, but would be great to have, but even if i just get the user id to notify of replies that is a start. |
Well, for starters, you table design needs work.
NEVER use a TEXT column unless you *MUST*. Certainly never for something like itemid or userid. TEXT columns are VERY expensive to store in the DB *and* they are even worse for querying against. They can't be indexed, for example. Anyway, are you looking for a list by both itemid and userid of how many have not been read? Or just for one specific itemid/userid? |
Oh...and don't be afraid of NULL columns!
It makes much more sense to have the [b]emailNotified[b] column there be a NULLable column that to have to provide an ugly '0000-00-00' fake date value! It's faster to test a NULL value, too. THat is: Code:
SELECT * FROM table WHERE field IS NULLCode:
SELECT * FROM table WHERE field = '0000-00-00 00:00:00' |
I think I have your answer. You'll need to try it with some real data. After you fix your table design, one hopes.
I created a simplified version of your table with only the relevant fields. Code:
mysql> select itemid, userid, dateAdded from itemComments order by itemid, dateAdded;Code:
mysql> SELECT C.itemid, C.userid, COUNT(*)Quote:
|
Whoops...code does not work if one user makes more than one comment on a single itemid.
Nuts. It counts the user's own later posts. Will think on it more. The queryy is trivial for a single itemid and single userid. For example: Code:
mysql> select COUNT(*) FROM itemcomments AS C, |
Well, I still haven't come up with a way to show you all users and all items.
But you *can* get a report on all itemid's for a single user. I added some more data to my test table and then ran the query you will see below. Code:
mysql> select itemid, userid, dateAdded from itemComments order by itemid, dateAdded;Is this good enough? Or do you really need it for all userid's and all itemid's?? |
And just to prove the point, here is the same thing for userid 100:
Code:
mysql> select C.itemid, COUNT(*) FROM itemcomments AS C, |
| All times are GMT +1. The time now is 09:53 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.