简体   繁体   中英

mysql union query

The table that contains information about members has a structure like:

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

Table for list of friends looks like:

  myid  |   date     |    user   
-------------------------------
    1   | 01-01-2011 |     4
    2   | 04-01-2011 |     3

I want to make a query that will as result print users from "friendlist" table that contains photos and names of that users from "members" table of both, myid (those who adding) and user (those who are added).

That table in this example will look like:

   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

This should do it:

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

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