3

我的数据库中有 3 个表,如下所示。这用于类似于foursqure的应用程序。我需要有关编写我在底部询问的 sql 语句的问题的帮助。

谢谢你

用户详细信息

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

朋友们

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

报到

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

我得到了与 8 和用户 8 为好友的用户的详细信息以及如下所示的签入位置

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

我如何编写 sql 来选择谁是 8 和用户 8 详细信息的朋友以及最后一次检查这些用户的位置

解释::

我寻求答案,例如

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

4 回答 4

2

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)
于 2012-08-01T04:18:12.143 回答
0

(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

于 2012-08-01T15:12:03.830 回答
0
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;
于 2012-08-01T04:18:52.107 回答
0

这不会打破关系,但它是回答您问题的一种直接方式。

编辑 我刚刚重新阅读了你的问题,我看到你也想要用户 8 的信息。目前尚不清楚您是否希望用户 8 作为单独的行或与朋友的行一致的信息。

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
    )

编辑 2 您的请求可能有点不清楚确定返回哪个签到位置。如果您想单独了解每个朋友的最新位置,请使用此选项。第一个查询在所有朋友中查找最近的位置。显然,这是同一主题的两种变体。

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
    )
于 2012-08-01T04:29:37.940 回答