So this seems like a simple little problem, but I haven’t been successful with getting this to work properly. I have a comments table, a posts table, and a notifications table. Previously I stored the postID in a column called “uniqueID” for notifications and for comments. Now I have changed it so that the comments unique id is stored in that column instead. So all I need to do is for each row that is a comment in notifications, go to the comments table where uniqueID==id and then return the value in that table called postID.
Here is a photo showing my question better.
http://i41.tinypic.com/nyzolg.png
The information from the photo is:
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 row in Posts;
Action = 2 means UniqueID identifies a row 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
But the UniqueID would stay the same where Action is 1.
My current query is this, and it worked fine before I changed my database values around.
$notificationsq = mysql_query("
SELECT
N.*,
P.*,
MAX(N.date) AS newDate
FROM
notifications N,
posts
WHERE
N.userID='$session'
AND
(
(
N.action='1'
AND
(N.state = 0 OR N.state=1)
)
OR
N.action='2'
)
AND P.state='0'
AND
N.uniqueID=P.id
GROUP BY
N.uniqueID
ORDER BY
N.state ASC,
newDate DESC
") or die(mysql_error());
Try adding “C.*” to your select clause, “comments C” to your from clause, and change ” N.action=’2′” to