I have two tables for notification and one table for friend_list. One notification tbale is that has the notification info who created and type and all and second which marks which friend has read the notification. the was able to retrieve the notification created by the user to be displayed only to user's friend. But now I want from the second table named notification read to display notification which are unread to the user's friend.
The query I made is (from notification + friend_list table)
$sql3=mysql_query("select n.*,f.friend_id,f.uid,f.status from notification n,friend_list f where f.uid='$id' and f.status='1' and n.title_text='Global Notification' and n.user_id in ('$id' , f.friend_id) and n.owner_user_id=f.friend_id order by n.time_stamp desc");
now how to get notification unread by friend?
The second table is
uid (friend_id)
notification_id
is_read
now I want a query with my existing query to get notifications unread by friends.
try this:
SELECT n.*,f.friend_id,f.uid,f.status
FROM notification n
INNER JOIN friend_list f
ON n.owner_user_id = f.friend_id
INNER JOIN second_table s
ON f.friend_id = s.uid AND
n.notification_id = s.notification_id
WHERE f.uid='$id' AND
f.status='1' AND
n.title_text='Global Notification' AND
n.user_id IN ('$id' , f.friend_id) AND
s.is_read = 0
ORDER BY n.time_stamp DESC;
Let's supose that:
'$id' is the id of user that raise notification
n.user_id is user that raises notification
n.owner_user_id is user that receive notification
The query is:
select
n.*,f.friend_id,f.uid,f.status
from
notification n
inner join
friend_list f
on n.owner_user_id=f.friend_id
inner join
notification_read nr
on nr.notification_id = n.notification_id and
nr.uid = f.friend_id
where
f.status='1' and
n.title_text='Global Notification' and
n.user_id = '$id' and
nr.is_read = 0
order by n.time_stamp desc
Edited due new OP comment.
Related
Here is my code. It is supposed to select the name of the person "to_whom" I'm sharing a particular file,
and the whole thing needs to return back the user_id (u_c.to_whom) and the user_name,
so I can populate my friends list with a checkboxe next to each other, that once clicked,
will share (save into a DB table) that particular file to a specific person.
This is from a web-app where users share files among each other.
SELECT u_c.to_whom, u.user_name
FROM files f
LEFT JOIN users_connections u_c
ON (u_c.who = :user_id OR u_c.to_whom = :user_id) AND u_c.friends = "Y"
LEFT JOIN users u
ON u.user_id = u_c.to_whom
WHERE f.file_id = 90
In the table files we have
file_id, file_name, file_desc, etc...
In the table users connections
id, who, to_whom, friends
1 1 4 Y
meaning that user 1 had initiated a friendship to user 4 and "Y" means the friendship is TRUE (N = still pending)
And in users we have
user_id, user_name
1 Jack
I cannot seem to get this working for some reason.
Can any advanced user help me out?
Thanks a bunch!!
I found the answer to this code. It is an essential code if you want to build a sharing file system.
$query_list_count = "SELECT COUNT(user_id)
FROM users_connections u_c
LEFT JOIN users u
ON u.user_id = u_c.who OR u.user_id = u_c.to_whom
LEFT JOIN files f
ON f.file_id = :file_id
WHERE (u_c.who = :user_id OR u_c.to_whom = :user_id AND friends = :friends)
GROUP BY u.user_id
";
$result_list_count = $db->prepare($query_list_count);
$result_list_count->bindValue(':user_id', $_SESSION['user_id'], PDO::PARAM_INT);
$result_list_count->bindValue(':friends', "Y", PDO::PARAM_STR);
$result_list_count->bindValue(':file_id', $file_id, PDO::PARAM_INT);
$count_ = $result_list_count->fetchColumn();
I am trying to left joins friend requests, but the way I am handing it, is like so, if the user sends someone a friend request the person sending the request's id goes in to the row 'friend_request_from' and the person receiving the friend request goes in to friend_request_to, now with my select, I am trying to left join requests on another person's id (not the session id) here's what I have:
SELECT * FROM timeline_likes
LEFT JOIN users
ON timeline_likes.timeline_likes_user = users.user_id
LEFT JOIN timeline_status
ON timeline_likes.timeline_likes_main_status = timeline_status.timeline_status_id
LEFT JOIN friends
ON timeline_likes.timeline_likes_user = friends.friends_friend
LEFT JOIN friend_request
ON timeline_likes.timeline_likes_user = friend_request.friend_request_to
LEFT JOIN friend_request
ON timeline_likes.timeline_likes_user = friend_request.friend_request_from
WHERE timeline_likes_main_status = ?
AND timeline_status.timeline_status_enabled = 'enabled'
GROUP BY users.user_id
ORDER BY timeline_likes_date ASC
Now my problem is, this will only select the sent friend requests friend_request.friend_request_from and not the received ones, how can I do something like this (I know what I will write below does not exist I just want to give you an example of what I need
LEFT JOIN friend_request
ON timeline_likes.timeline_likes_user = friend_request.friend_request_from
OR timeline_likes.timeline_likes_user = friend_request.friend_request_to
Because depending on if they sent or received depends on which row their id will go in to.
I have 3 tables. User, alert and pref. User is obviously my user info table. Alert is my table where I store user alert/notification settings and pref is where I store my user preferences.
Not all users will have entries for prefs and alerts. When I'm trying to make a call for my alerts I'm running into trouble with my SQL knowledge.
I'm trying to do a 3 way join on user, alert and pref where the uid's are the same. I want to Select all user.id and ignore all uids that have an alert.deleted and pref.deleted = 1
user | alert | pref
id uid uid
alert_id pref_id
deleted deleted
One way to do that is
SELECT id
FROM user
WHERE user.id not in (SELECT uid FROM alert WHERE deleted = 1)
AND user.id not in (SELECT uid FROM pref WHERE deleted = 1);
But if you meant to say you want all users that actually have an alert and pref, just not the ones that are deleted, you could use
SELECT id
FROM user
INNER JOIN pref
ON user.id = pref.uid
AND pref.deleted = 0
INNER JOIN alert
ON user.id = alert.uid
AND alert.deleted = 0;
In your question you are talking about getting the alerts, so maybe there aren't any prefs necessarily for a particular user, but you do want all the not-yet-deleted alerts, then use
SELECT id
FROM user
LEFT OUTER JOIN pref
ON user.id = pref.uid
AND pref.deleted = 0
INNER JOIN alert
ON user.id = alert.uid
AND alert.deleted = 0;
Take your pick.
SELECT id
FROM user u
WHERE NOT EXISTS (SELECT 1 FROM alert WHERE uid = u.id AND deleted = 1)
AND NOT EXISTS (SELECT 1 FROM pref WHERE uid = u.id AND deleted = 1)
;
I have created 3 tables:
Login(firstname,lastname,age,gender,email)
Scraps(email,scrap,posttime)
requests(email,frnemail,frnshpstatus,frnpic)
I want a query that connects these three tables to show scraps posted by user and by the people whose frnshpstatus is 'Y' with respect to his his email in request table.
You can try a join query.
DECLARE #usrEmail varchar(255) // Set the users email you are getting.
SELECT l.*, s.scrap, s.posttime
FROM Login l
LEFT JOIN Scraps s
ON l.email = s.email
WHERE l.email = #usrEmail
// Use a UNION query to also get scraps left by the users friend.
// Im assuming the friends would have entries in the login column as well.
UNION ALL
SELECT l.*, s.scrap, s.posttime
FROM Login l
LEFT JOIN Scraps s
ON l.email = s.email
WHERE l.email IN (SELECT frnshpEmail FROM requests WHERE email = #usrEmail AND frnshpStatus = 'Y') // This will create a list of all the users that are also have a frnshp with the user.
I'm not sure how you are using the requests table. If you can give me more details, I'll try to help with the specifics, but with what I have, this should give you an idea of what to do.
Try this:
select * from scraps where email in(
select r.frnemail
from [Login] l inner join requests r
on l.email=r.email and r.frnshpstatus = 'Y'
where l.email=#useremail)
I'm writing a basic message-script in PHP/MySQL but I'm stuck at a database query right now. I'll appreciate any hints or assistance (:
I'm using two tables, since a message can be sent to several users:
messages:
id | sender_id | subject | ...
message_receivers:
message_id | receiver_id | ...
What I want to do now is display a message to the user that he selects. But I want to show the whole message history the user had in that conversation (jumping in browser to the one he selected). Doing this with a join is quite simple:
SELECT * FROM messages
JOIN message_receivers
ON messages.id = message_receivers.message_id
WHERE sender_id = x
AND receiver_id = y
But now I'm missing the information of other receivers of a message! And I have no clue how to get this information. Any ideas for that? (:
Join the message_receivers table one more time to retrieve the other recipients of the message:
SELECT
m.id, m.sender_id, m.subject,
r.receiver_id AS recipient,
c.receiver_id AS carboncopy
FROM messages AS m
INNER JOIN message_receivers AS r
ON m.id = r.message_id
LEFT OUTER JOIN message_receivers AS c
ON r.message_id = c.message_id AND r.receiver_id != c.receiver_id
WHERE m.sender_id = x AND r.receiver_id = y
The recipient that your are interested in will be in column recipient (in every result record). Other recipients will be in column carboncopy (one per result record). If carboncopy is NULL, then the message had only a single receiver.
If you want to see all the receivers of a message then remove the second part of the were clause:
AND receiver_id = y
at the same time you will want to specify the message_id because this will be to confusing to the user on the front end
AND message_id = z
You're missing information about other receivers of the message because of the clasue:
AND
receiver_id = y
This restricts the result set to just receiver y. Remove the clause, and you'll get them all. However you'll probably get every message sent where sender_id = x as well, so you'll need to limit the query by specifying a message_id.
So your final query should look something like this:
SELECT
*
FROM
messages
JOIN message_receivers ON messages.id = message_receivers.message_id
WHERE
sender_id = x
AND
message_id = y
you don't need to restrict your result to receiver_id = y, do you?
Also you might write the statement in a different way and easily return receivers ids:
SELECT m.*, r.receiver_id
FROM messages m, message_receivers r
WHERE m.id = r.message_id
AND m.sender_id = x