怎么写sql语句?

时间:2012-08-01 04:08:03

标签: mysql

我的数据库中有3个表如下。这用于像foursqure一样的应用程序。我需要帮助解决编写我在底部问过的sql语句的问题。

谢谢

user_details

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

朋友

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

我得到的用户是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 个答案:

答案 0 :(得分:2)

将其加入由以下表格返回的表格:

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

这应该为每个用户提供一个条目,并且因为它按日期反向排序,所以该条目应该是最新的。

  

但是当我按照它分组给我第一个日期而不是最新日期

这个怎么样:

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

答案 1 :(得分: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;

答案 2 :(得分: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
    )

答案 3 :(得分: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)为a,user_details为b WHERE a.user_id = b.user_id AND(a.user_id in(从朋友中选择user_idf,其中user_idff ='8'union select user_idff from friends where user_idf ='8')或a.user_id = 8) GROUP BY a.user_id)

上面的查询给了我所需的答案。

谢谢大家给予的帮助