Query for associated tables?

Source: Internet
Author: User
For example
Table A (category)
label_id Label_name
1 Badminton
2 Basketball
3 Football
4 Pingpong Ball

b table
ID label_id UID
1 1 1
2 2 1
3 3 1
4 2 2
5 4 2
6 1 3
7 4 3

C table
UID name Sex
1 Three men
2 Li Shiju
3 Harry Male
4 Zhaoliu

Now, the user clicks on a table value to display the records of the C table in table B and displays the value of table B.
For example, the user clicks on the Badminton in Table A (label_id is 1), according to table B that has a UID of 1 and 3 likes badminton (label_id 1 is 1 and 3), then displays all the properties of the C table (UID 1 and 3) Zhang San and Harry, And show all Zhang San and Harry like it (according to the B table). The requirements show the following results:

1, Zhang San male badminton basketball Football
2, Harry male Badminton Soldier Pong Ball

I'm using a thinkphp. How do I get it?

Reply to discussion (solution)

It can be understood that I want to know which ones like badminton, show everyone who likes badminton, and show them all the sports they like.

$sql = "Select C.*,group_concat (a.label_name) as sport from a INNER join B in a.label_id=b.label_id INNER join C on b.uid=c. UID WHERE b.uid in (SELECT b.uid from a INNER JOIN b to a.label_id=b.label_id WHERE a.label_id = 1) GROUP by UID "; M ()->query ($sql);

Good cow, ox man!!

  • Related Article

    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.