Data Table Association Query

Source: Internet
Author: User
I have two tables, table A is a commodity table, table B is the message table is the user's comments on the product,
A table ID (self-increment ID) name (product name) money (commodity price) B table ID (self-increment ID) content (message content) PID (corresponding Product ID) UID (user ID)

Product ID Product Name Commodity price message number
xxx xxx xxx xxx
xxx xxx xxx xxx

< 1 2 3 4 5 6 >



To traverse out and display the page, the main is how the data tables are correlated to get the data and then form a new two-dimensional array.


Reply to discussion (solution)

Select a.ID as aid,a.name as Aname,a.money as amoney,b.id as bid from TableB as B left joins table as a on b.pid=a.id where Condition ORDER BY Time DESC limit 0,10//per page 10 article, Pagenow page
This is already a two-dimensional array.

Select A.*,ifnull (b.c,0) from a LEFT join (select Pid,count (0) c from B group by PID) b to a.ID = b.pid ORDER BY a.id Desc L Imit 0, 10;

Thank you, everyone's enthusiastic answer

  • 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.