1. A table with user Information table users (User_id,nickname), the other table contact form contacts (user_id,friend_id). Now, to find out what the contact is about.
SELECT U1.nickname as myselft,u2.nickname as friend from contact C inner JOIN user U1 on c.user_id=u1.user_id inner JOIN U Ser U2 on C.friend_id=u2.id
If friend_id may be empty, then the latter inner join will be changed to a left join.
2. Find out the friends and their own posts, post table invitation (id,owner_id,title,content)
SELECT * FROM (Invitation I inner JOIN user U in u.user_id=i.owner_id) inner join (select friend_id from contacts where US er_id=2 UNION ALL Select 2) as B on b.friend_id=i.user_id
Do not use subqueries in MySQL, the previous version of MySQL 5.5 is very inefficient for sub-queries.
This article is from "Maple Night" blog, please be sure to keep this source http://fengwan.blog.51cto.com/508652/1672746
How to do several SQL statements