包含成员信息的表具有如下结构:

   id   |    fname    |    pic    |   status
--------------------------------------------------
   1    |     john    |   a.jpg   |     1
   2    |     mike    |   b.jpg   |     1
   3    |     any     |   c.jpg   |     1
   4    |     jacky   |   d.jpg   |     1

朋友列表表如下:
  myid  |   date     |    user
-------------------------------
    1   | 01-01-2011 |     4
    2   | 04-01-2011 |     3

我想做一个查询,结果将打印“friendlist”表中的用户,该表包含来自“members”表中的用户的照片和名称,包括myid(添加的用户)和user(添加的用户)。
本例中的表将如下所示:
   myid   |   myidname  |    myidpic   |  user  |  username  |  userpic  |  status
 -----------------------------------------------------------------------------------
     1    |     john    |    a.jpg     |   4    |    jacky   |   d.jpg   |     1
     2    |     mike    |    b.jpg     |   3    |    any     |   c.jpg   |     1

最佳答案

这应该做到:

SELECT
    m1.id AS myid,
    m1.fname AS myidname,
    m1.pic AS myidpic,
    m2.id AS user,
    m2.fname AS username,
    m2.pic AS userpic,
    m1.status
FROM
    members m1
        INNER JOIN friends f ON m1.id = f.myid
        INNER JOIN members m2 ON f.user = m2.id

关于php - MySQL联合查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4625190/

10-17 03:05