Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8798635
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T00:08:24+00:00 2026-06-14T00:08:24+00:00

I have a fairly complicated notification system that I am working on, it’s quite

  • 0

I have a fairly complicated notification system that I am working on, it’s quite simple for the most part, the complexity gets into checking other tables whether or not the content has been removed or not, and I have multiple tables of data that all goes into the notification system, so it has to check each of those, and I have to be able to easily be able to add more tables as I build the website to allow other notification types to be added, and with the current query i am using it does not allow that, I have tried to come up with a better way of doing this, however I am not good enough with mysql queries to do so, so I am hoping I could get some (or it may turn out to be alot) of assistance by one (or some) of you.

Here’s my current query:

SELECT
    n.*,
    MIN(n.state) state,
    MIN(n.status) status,
    MAX(n.date) maxDate
FROM notifications n
LEFT JOIN comments c 
     ON n.type = 'comment' 
     AND n.uniqueID = c.id  
     AND c.state='0'
LEFT JOIN posts p 
     ON n.type = 'post' 
     AND p.id = n.uniqueID OR n.type = 'comment' 
     AND p.id = c.postID

WHERE n.toID = '$session'
AND p.state = 0
GROUP BY n.uniqueID
ORDER BY status ASC, maxDate DESC

So what the above statement does, in a nutshell, is this. It selects notifications from the notification table, in this table there is a column for type and uniqueID of that content. So for each row that’s type is posts it checks the post table where the uniqueID from notifications is the same as the unique id in the posts table, which it then checks the state of that post to see if it has been deleted or not (if it has not been removed, state=0, else I don’t want it). The same goes for comments.

So I need a way to do everything just like this does it, however I need it to be easier to continuously add new table checks to it. I have been trying to add another left join for a table called likes, however i am unable to get it to work together, for whatever reason. However I have come up with this code that will do what I want it to. (only for the likes table though, it does not include any other table which i need it to)

SELECT
    n.*,
    MIN(n.state) state,
    MIN(n.status) status,
    MAX(n.date) maxDate
FROM notifications n
LEFT JOIN likes l 
     ON n.type = 'likes' 
     AND n.uniqueID = l.id 
WHERE n.toID = '$session'
AND l.state='0'
GROUP BY n.uniqueID
ORDER BY status ASC, maxDate DESC

I hope someone can help me out with this solution, thank you so much in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T00:08:25+00:00Added an answer on June 14, 2026 at 12:08 am

    In looks like you could use an “ANY” subquery
    http://dev.mysql.com/doc/refman/5.0/en/any-in-some-subqueries.html

    SELECT
        n.*,
        MIN(n.state) state,
        MIN(n.status) status,
        MAX(n.date) maxDate
    FROM notifications n
    WHERE n.toID = '$session' AND
        (
            (
                n.type = 'likes' AND
                n.uniqueID = ANY (
                    SELECT likes.id
                    FROM likes INNER JOIN posts on (likes.postId=posts.id)
                    WHERE likes.state='0' and posts.state='0'
                )
            ) OR
            (
                n.type = 'comment' AND
                n.uniqueID = ANY (
                    SELECT comments.id
                    FROM comments INNER JOIN posts on (comments.postId=posts.id)
                    WHERE comments.state='0' and posts.state='0'
                )
            ) OR
            (
                n.type = 'post' AND
                n.uniqueID = ANY (SELECT id FROM posts WHERE state='0')
            )
        )
    GROUP BY n.uniqueID
    ORDER BY status ASC, maxDate DESC;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a fairly complicated installer that I'm writing in Wix that has a
I am working on a fairly complicated project. We have a c# forms app
Just out of curiosity, for applications that have a fairly complicated module tree, would
I have a simple single threaded utility written in C# that inserts data into
I have a project that has a fairly complicated, nested (references calling the referenced
I have a build process that creates an ear in a fairly complicated manner
I have a fairly complicated hierarchy of types and sometimes run into hard-to-debug type
I have a SQL query that's fairly complicated and involves multiple subqueries and unions.
I have an object under test that makes a fairly complicated call to a
I have a site that has a fairly complicated footer, see http://www.roadsafetyforchildren.co.uk/ , not

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.