tags:

views:

68

answers:

2
+1  Q: 

mysql query help?

(
  SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
  FROM   relationships r
  JOIN notes m ON m.user_id = r.leader
  JOIN user u ON r.leader = u.user_id
  WHERE r.listener ='2'
)


UNION ALL
(
  SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
  FROM   notes m
  JOIN user u ON m.user_id = u.user_id
  WHERE u.user_id ='2'
)
WHERE dt > '2010-09-20_131830'
ORDER BY dt DESC

what this deos is it queries the user(loggedin), people they follow and retrives thier messages. tables are shown below,!

what i want to add to this query, is also the user(loggedin) updates aswell. but obviously the user deosnt follow himself. whats the best way i can do this!!

user_table

 user_id | username
 -------------------
       1 | solomon
       2 | muna

message table

id    user_id| message
---------------------------------------
 1       1   | this is my first message
 2       1   | this is my second message
 3       2   | this is muna message    

relationship table

leader | follower
------------------
     1 | 2
+2  A: 

The most obvious solution would be to UNION ALL your query, with another similar query that retrieves the messages of the logged in user. Something like this:

(
  SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
  FROM   relationships r
  JOIN   notes m ON m.user_id = r.leader
  JOIN   user u ON r.leader = u.user_id
  WHERE  r.listener ='".$_SESSION['user_id']."'
)
UNION ALL
(
  SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
  FROM   notes m
  JOIN   user u ON m.user_id = u.user_id
  WHERE  u.user_id ='".$_SESSION['user_id']."'
);

UPDATE:

Following the updated query, you cannot have a WHERE clause out there. You should either have a separate WHERE for each query, or else use a derived table like this:

SELECT *
FROM
((
  SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
  FROM   relationships r
  JOIN   notes m ON m.user_id = r.leader
  JOIN   user u ON r.leader = u.user_id
  WHERE  r.listener ='".$_SESSION['user_id']."'
)
UNION ALL
(
  SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
  FROM   notes m
  JOIN   user u ON m.user_id = u.user_id
  WHERE  u.user_id ='".$_SESSION['user_id']."'
)) d
WHERE    d.dt > '2010-09-20_131830'
ORDER BY d.dt DESC;
Daniel Vassallo
thats what i was using aswell, but i thought thier might be another way!!
getaway
whats the difference between `union` and `union all`
getaway
@gateway: It's a bit faster than `UNION`, because it doesn't remove duplicates. If I understand correctly, you shouldn't have duplicates in those two queries.
Daniel Vassallo
@daniel thanks!!!
getaway
Sorry, I cringe at the sight of ANSI-89 join syntax, but +1
OMG Ponies
if i wanted to order the whole results by Dt which is date, would i do that for each query or just at the end
getaway
@OMG: Yes, even me. It's so 80s :)
Daniel Vassallo
@getaway: Yes at the end of the query, assuming you want a twitter-like timeline. If you put an `ORDER BY` for each query, you would order them separately, and the logged-in user's messages will be returned at the bottom.
Daniel Vassallo
OMG Ponies
i changed the above query, but it gives me an error, which i check on conditions for the whole query, but the order by is working.
getaway
@gataway: Updated my answer :)
Daniel Vassallo
@daniel `#1248 - Every derived table must have its own alias`
getaway
@gataway: Note the little `d` after the last parenthesis in my answer :) ... That's the alias. I guess you missed that :)
Daniel Vassallo
`#1248 - Every derived table must have its own alias,` :))) i still get the same error
getaway
@getaway: You're right. I had to enclose everything in parenthesis :) Fixed the answer.
Daniel Vassallo
thanks @daniel your a genius
getaway
+2  A: 

How about this:

SELECT u.username, u.picture, m.id, m.user_note, m.reply_id, m.reply_name, m.dt
FROM notes m
INNER JOIN user u
ON m.user_id = u.user_id
WHERE (m.user_id = '".$_SESSION['user_id']."'
OR m.userID IN (
   SELECT r.leader
   FROM relationships r
   WHERE r.listener ='".$_SESSION['user_id']."'))
Jeremy Goodell
I think that won't work because of the `INNER JOIN` with `relationships`.
Daniel Vassallo
You're right, I changed it to get rid of the join.
Jeremy Goodell
this is much cleaner!!!, i like this one
getaway
this duplicates the records though
getaway
I think I left out the notes to user join -- try it now.
Jeremy Goodell
very nice!!! how would i retrieve the whole results say for insatnce with this clause`WHERE dt > '2010-09-20_131830`
getaway
WHERE (m.user_id = '".$_SESSION['user_id']."'OR m.userID IN ( SELECT r.leader FROM relationships r WHERE r.listener ='".$_SESSION['user_id']."'))AND dt > '2010-09-20_131830'
Jeremy Goodell