Look at a multi-Table query Problem

Source: Internet
Author: User
The data table activity contains fields such as ac_idac_titleopenid and ap_idac_id in the apply field) openid and other fields now need to first find out that the application has a field that matches the user's openid, and then query the ac_id in the apply table... the data table activity contains fields such as ac_id ac_title openid.
The data table apply contains fields such as ap_id ac_id (ac_id in the activity table) openid.

Now we need to first check that the "apply" field matches the user's openid, and then query the dataset where the "ac_id" in the "apply" table is equal to the "ac_id" in the activity table.

query("select * from activity,apply where apply.ac_id=activity.ac_id order by ap_id desc");

Such a statement can only query the dataset where the "ac_id" in the "apply" table is equal to the "ac_id" in the "activity" table, but does not know how to find the data where the "openid" in the "apply" table is equal to the user's openid. the great god proposed a solution. The younger brother is a beginner.

Reply content:

The data table activity contains fields such as ac_id ac_title openid.
The data table apply contains fields such as ap_id ac_id (ac_id in the activity table) openid.

Now we need to first check that the "apply" field matches the user's openid, and then query the dataset where the "ac_id" in the "apply" table is equal to the "ac_id" in the activity table.

query("select * from activity,apply where apply.ac_id=activity.ac_id order by ap_id desc");

Such a statement can only query the dataset where the "ac_id" in the "apply" table is equal to the "ac_id" in the "activity" table, but does not know how to find the data where the "openid" in the "apply" table is equal to the user's openid. the great god proposed a solution. The younger brother is a beginner.

You only need to add the openid condition to the SQL statement, that is, add and openid = 'xxx' as follows'

select * from activity,apply where apply.ac_id=activity.ac_id and apply.openid='xxx' order by ap_id desc

Assume that the openid of the user to be searched is 2, which means:

1. Search openid = 2 from the activity of the data table as the Dataset 1.

2. for data in Dataset 1, find the data of apply. ac_id = Dataset 1. ac_id in the apply data table?

How about left connection?
Select * from activity as ac left join apply as ap on ac. ac_id = ap. ac_id where openid = "xxxxx"

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.