简体   繁体   中英

how to write the sql statement?

I have 3 tables as follows in my database. this is used to a application just like foursqure. i need help with the problem of writing the sql statement i have asked in the bottom of this.

thank you

user_details

user_id | fname
----------------
   1    | Losh
   8    | Dush
   9    | Rosh
  10    | NELL

friends

user_idf |user_idff
----------------
   1     |   8
   8     |   9
   10    |   1

Check_in

check_in_id |user_id | place    | date
--------------------------------------------
       1    |   8    | Hotel    | 01/01/2012
       2    |   9    | Home     | 05/01/2012
       3    |   1    | Junction | 08/01/2012
       4    |   1    | Rest     | 11/01/2012
       5    |   9    | Hotel    | 15/01/2012
       6    |   8    | Home     | 15/01/2012

i get the user's who are friends with 8 and user 8 details AND the check in places as follows

SELECT a.`user_id`, a.`fname` , b.*
FROM `user_details` a, `check_in` b
WHERE (b.user_id = 8 
OR b.user_id in (select user_idf from friends where user_idff = '8' union select user_idff from friends where user_idf = '8')) AND b.user_id = a.user_id

how do i write the sql to select who are friends with 8 and user 8 details AND the last check in place of those users

explanation::

i seeks for a answer such as

user id    name     place   date
   1       LOSH     Rest  11/01/2012
   8       DUSH     HOME  15/01/2012
   9       ROSH     HOTEL 15/01/2012

Join it to the table returned by:

(SELECT `user_id`, `place` FROM Check_in GROUP BY user_id ORDER BY `date` DESC)

That should give you one entry per user, and since it's sorted in reverse by date, that entry should be the most recent.

But when i group by it gives me the first dates not the latest date

How about this:

(SELECT user_id, place
   FROM (SELECT * FROM Check_in ORDER BY `date` DESC) tmp
   GROUP BY user_id)
SELECT user_id, fname, c.place
FROM user_details u
     INNER JOIN (SELECT IF(user_idff = 8, user_idf, user_idff) AS user_id
                 FROM friends
                 WHERE (user_idff = 8 OR user_idf = 8)
                ) f
                ON u.user_id = f.user_id
     LEFT JOIN (SELECT c1.user_id, place
                FROM Check_in c1
                     LEFT JOIN  Check_in c2
                         ON c1.user_id = c2.user_id AND
                            c1.date < c2.date
                WHERE c2.date IS NULL
               ) c
               ON u.user_id = c.user_id;

This doesn't break ties but it's a straighforward way of answering your question.

EDIT I just re-read you question and I see that you want user 8 info also. It's not clear whether you want user 8 as a separate row or with info in line with the friends' rows.

select *
from
    friends as f inner join check_in as ci on ci.user_id = f.user_idff
    inner join user_details as ud on ud.user_id = f.user_idff
    inner join user_details as ud8 on ud8.user_id = f.user_idf
where
    f.user_idf = 8
    and date = (
        select max(date)
        from friends as f2 inner join check_in as ci on ci.user_id = f2.user_idff
        where f2.user_idf = f.user_idf
    )

EDIT 2 You request may be a small bit unclear about determining which check-in location to return. Use this option if you want the latest location of each friend individually. The first query finds the most recent location among all friends. Obviously these are two variations on an identical theme.

select *
from
    friends as f inner join check_in as ci on ci.user_id = f.user_idff
    inner join user_details as ud on ud.user_id = f.user_idff
    inner join user_details as ud8 on ud8.user_id = f.user_idf
where
    f.user_idf = 8
    and date = (
        select max(date)
        from check_in as ci
        where ci.user_id = f.user_idff
    )

(SELECT a.user_id, a.place, b.fname, a.date, a.time, a.check_in_id FROM (SELECT * FROM check_in ORDER BY date DESC) as a, user_details as b WHERE a.user_id = b.user_id AND (a.user_id in (select user_idf from friends where user_idff = '8' union select user_idff from friends where user_idf = '8') OR a.user_id = 8) GROUP BY a.user_id)

above query gave me the required answer.

thank you all for the help given

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM