A Combined Query column in MySQL

Source: Internet
Author: User
A MySQL Union query column is very basic, but very practical.

First read the data table. Data Table class:
-------------------------------
Id cname
-------------------------------
1 men's wear
2 women's leather pants
3 female Cotton
-------------------------------
The class Table stores the product's category name and its serial number.

Data Table product:
-------------------------------
PID ptype pparent
-------------------------------
1 A01 1
2 A21 1
3 B10 2
4 C11 3
5 c01 2
-------------------------------
The product table stores the product name and its category ID (pparent)

All product numbers, names, and categories must be displayed as follows:
-------------------------------
Sequence Number category
-------------------------------
1 A01 men's wear
2 A21 men's wear
3 B10 Women's leather pants
4 C11 Female Cotton
5 c01 Women's leather pants
-------------------------------

Here we will query the product table and read the pname field of the corresponding class table based on the pparent field. The most traditional one can be implemented using two select statements, but the following is a simple implementation of a combined query statement in MySQL:
Select Product. *, class. * from product inner join class on product. pparent = Class. ID where product. PID is not null
In PHP, the records queried in this way are placed in the array, for example:
$ Myrow = mysql_fetch_array (mysql_query ($ SQL ));
In the $ myrow array, fields in product are stored first, and class fields are stored later.
Use print_r to print $ myrow. The result is as follows:
Array (
[0] => 1
[Pid] => 1
[1] => A01
[Ptype] => A01
[2] => 1
[Pparent] => 1
[3] => 1
[ID] => 1
[4] => men's wear
[Cname] => men's wear
......
)

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.