How to do several SQL statements

Source: Internet
Author: User
Tags contact form

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.