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 7032943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T00:59:57+00:00 2026-05-28T00:59:57+00:00

I’ve been trying to get this complex MYSQL query to work exactly right over

  • 0

I’ve been trying to get this complex MYSQL query to work exactly right over the last few days, of course because it’s got so many aspects to it that effect it it’s difficult to surely know that it’s 100% working correctly, and I’m not at all good with more complex MYSQL queries. also this query that I have now is quite messy, so the data it returns is a little scattered, and I’m not exactly sure how to address this issue. I’ve read through MYSQL Join and everything and I sort of understand it, but I’m not sure which to use in my case, and how to use them properly.

Here’s my current query that works as it should. (just needs cleaning up, I think, so I don’t have to have redundant values)

$notificationsq = mysql_query("SELECT
N.*,
N.fromID,
N.date,
N.id AS ID,   //I have to do this because if I don't it doesn't return anything, 
              ///I guess because it joins 3 tables with the id column. not sure 
              ///how to call the correct data.
MIN(N.state) AS State,
MAX(N.date) AS newDate,
P.*,
C.*,
P.id AS uniqueID
FROM notifications N
LEFT JOIN comments C ON N.action = 2 AND N.uniqueID = C.id AND C.state=0
LEFT JOIN posts P ON N.action = 1 AND P.id = N.uniqueID 
OR N.action = 2 AND P.id = C.postID
WHERE N.userID = '$session'
AND (N.action = 1 AND N.state IN (0, 1) OR N.action = 2)
AND P.state = 0

GROUP BY P.id
ORDER BY
State ASC,  
newDate DESC


 ") or die(mysql_error());

My table structure:

Table: notifications

id  UserID  FromID  UniqueID    Action  State   Read_Date   Date
1   1       2       1           1       0       0           1325993600
2   1       6       2           1       0       0           1325993615
3   1       2       1           2       0       0           1325993622
4   1       6       2           2       0       0           1325993661
5   2       6       2           2       0       0           1325993661

Action = 1 means UniqueID identifies a column in Posts;
Action = 2 means UniqueID identifies a column in Comments.

Table: posts

id  ToID    FromID  Post        State   Date
1   1       2       Hey         0       1325993600
2   1       6       okay yeah   0       1325993615

Table: comments

ID  PostID  FromID  Comment     State   Date
1   1       2       lol         0       1325993622
2   1       6       ohh         0       1325993661

So, in the Notifications table where action is 2, the UniqueID’s are for the ‘id’ in the Comments table.
What I want to return is the PostID, so in the query it would just be as if the UniqueID was this instead:

1
2
1
1
1
  • 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-05-28T00:59:57+00:00Added an answer on May 28, 2026 at 12:59 am

    If your state = 0 filter limits the connection to the comments, then the inner join on the posts could filter out the result, try making that a left join as well to test.

    You should have a prefix on your ORDER BY clause (either ORDER BY P.State or N.State).

    The reason you’re having an error with the N.id is that the id is already selected with the N.*

    You’re better to use an ENUM type to deal with multiple states. This leads to more readable SQL with the same performance (ie. N.action = ‘add’, instead of 2)

    Avoid any select *, it’s error prone and the performance isn’t as good as the manual alternative.

    As far as cleaning up, I find it much easier to read with clean whitespace and names:

    SELECT notifications.*
         , notifications.fromID
         , notifications.date
         , MIN(notifications.state) AS State
         , MAX(notifications.date) AS newDate
         , posts.*
         , comments.*
         , posts.id AS uniqueID
    FROM notifications
    LEFT JOIN comments ON notifications.action = 2
                      AND notifications.uniqueID = C.id
                      AND comments.state = 0
    LEFT JOIN posts ON (notifications.action = 1 AND posts.id = notifications.uniqueID)
                    OR (notifications.action = 2 AND posts.id = comments.postID)
    WHERE notifications.userID = '$session'
      AND (notifications.action = 1 AND notifications.state IN (0, 1) OR notifications.action = 2)
      AND posts.state = 0
    GROUP BY posts.id
    ORDER BY notifications.State ASC
           , newDate DESC
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this is what i have right now Drawing an RSS feed into the php,
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I have a jquery bug and I've been looking for hours now, I can't
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Does anyone know how can I replace this 2 symbol below from the string

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.