MySQL从每个表中获取用户的最新数据

时间:2017-09-05 01:47:00

标签: php mysql sql database

我有6个表来存储我的用户数据

tbl_user_name

uuid | username | date (unix time)
-----------------------------------------
  0  |  lisha.s     |  1489857236
  1  |  titami      |  1485853232
  2  |  mathew      |  1442853636 <----|> Users can change their username
  3  |  sandarjun   |  1489857239      |> so i need to get the uuid by the
  2  |  mathew_kar  |  1575456274 <----|> newest username which is given

tbl_user_fullname:

uuid |    fullname      | date (unix time)
-----------------------------------------
  0  |  Lisha Simonette |  1489857236
  1  |  Titus Amiran    |  1481256345
  2  |  Mathew Karolina |  1489234455
  3  |  Sandhya Arjun   |  1489857239
  0  |  Lisha Karolina  |  1599999999

tbl_user_website:

uuid | website  | date (unix time)
-----------------------------------------
  0  |  google.com        |  1489857236
  1  |  titusamiran.com   |  1489855234
  2  |  mathewk.net       |  1489857432
  3  |  blod.sandhya.info |  1489857239

tbl_user_birthdate:

uuid |   birthdate   | date (unix time)
-----------------------------------------
  0  |  02-05-1991   |  1489857236
  1  |  05-08-1980   |  1489857123
  2  |  09-09-1992   |  1489851334
  3  |  17-02-1998   |  1489857239

tbl_user_follower:

uuid |   follower_uuid
-----------------------
  0  |  4  
  1  |  8  
  2  |  0 
  3  |  4
  3  |  2
  3  |  1

tbl_user_online:

uuid |   last_seen (unix time)
-----------------------
  0  |  1489855334  
  1  |  1589851111  
  2  |  1689857234
  3  |  1789834539

我想收集具有给定用户名的用户的uuid,全名,网站,生日和关注者数量。 (我需要获取用户名的最新uuid,因为用户可以更改用户名)。

日期列是更改值时的时间戳。例如在tbl_user_fullname中:Lisha Simonette(uuid 0)与Mathew Karolina(uuid 2)结婚,因此我需要在日期栏中获得Lisha(uuid 0)的新全名。等等......对于tbl_user_websitetbl_user_birthdate ..即使他们不经常改变他们的生日;)

从表tbl_user_online我只需要last_seen时间戳。

我给查询的值是用户名。用户名应该给出我可以查询其他表的uuid。

非常感谢你的帮助,抱歉我的英语不好;)

1 个答案:

答案 0 :(得分:1)

以下查询将解决问题:

SELECT usr.uuid,
       ful.fullname,
       web.website,
       brt.birthdate,
       COUNT(fol.follower_uuid)
  FROM tbl_user_name       usr
  JOIN tbl_full_name       ful ON ful.uuid = usr.uuid
  LEFT JOIN                
       tbl_full_name      ful2 ON ful2.uuid = ful.uuid
                              AND ful2.time_stamp > ful.time_stamp
  JOIN tbl_user_website    web ON web.uuid = usr.uuid
  LEFT JOIN                
       tbl_user_website   web2 ON web2.uuid = web.uuid
                              AND web2.time_stamp > web.time_stamp
  JOIN tbl_user_birthdate  brt ON brt.uuid = usr.uuid
  LEFT JOIN 
       tbl_user_birthdate brt2 ON brt2.uuid = brt.uuid
                              AND brt2.time_stamp > brt.time_stamp
  JOIN tbl_user_follower   fol ON fol.uuid = usr.uuid
 WHERE usr.username = 'jack_2'
   AND ful2.uuid IS NULL
   AND web2.uuid IS NULL
   AND brt2.uuid IS NULL
 GROUP BY
       usr.uuid,
       ful.fullname,
       web.website,
       brt.birthdate

此查询通过隔离共享行中公共元素的表中的最新时间戳来工作。在这种情况下,uuid。

Here is the fiddle to see it working.

Here is a fiddle without the username filter

但是,您需要解决一个更大的问题。您不应该像使用时间戳一样存储值。

我建议您查看table triggers以获取更新和插入语句。您应该设置一个触发器,以自动将条目插入到存储此信息的审计表中,同时保持核心功能表的小巧有序。

例如创建一个名为tbl_user_name_audit的表。在tbl_user_name上触发更新。更新用户名时,先前的值将插入到tbl_user_name_audit表中,其中包含before和after值以及时间戳和审计类型(INSERT / UPDATE / DELETE)。