In the MYSQL joint query, GROUP_CONCAT, MAX, and year use _ MySQL

Source: Internet
Author: User
In the MYSQL joint query, GROUP_CONCAT, MAX, and year use bitsCN.com

This is what happened yesterday. it was encountered when I wrote a resume searcher. I checked a lot of information. here I will record some of the problems encountered in the SELECT joint query with a solution.

The first example is as follows:

$ SQL = "";

$ SQL. = "SELECT ";

$ SQL. = "". T _. "person_info.personid ,";

$ SQL. = "". T _. "person_info.personname ,";

$ SQL. = "". T _. "person_info.sex ,";

$ SQL. = "year (". T _. "person_info.birthday) AS age ,";

$ SQL. = "MAX (". T _. "person_edu.education) AS xueli ,";

$ SQL. = "". T _. "person_edu.school ,";

$ SQL. = "GROUP_CONCAT (". T _. "person_edu. 'subobject '),";

$ SQL. = "". T _. "person_info.shi1 ";

$ SQL. = "FROM ";

$ SQL. = "". T _. "person_info ";

$ SQL. = "LEFT JOIN". T _. "person_edu ON". T _. "person_info.personid =". T _. "person_edu.personid ";

$ SQL. = "WHERE 1 = 1 ";

$ SQL. = "AND". T _. "person_info.personid = '". $ autoid ."'";

$ SQL. = "GROUP BY personid ";

GROUP_CONCAT () is a function provided BY the MySQL database. it is usually used together with group by to form a result set composed of all the subject results of the same personid and separated BY commas, only fields of the character type are supported. Special writing is required for the integer type, because it is not studied.

MAX is the maximum value in the integer type. it is used here to obtain the highest education level (educational level Dictionary). it is used together by group.

Year and month are all extracted from the time type, such as year and month. here I extract year from the birth day to calculate the age (the so-called year after year, the age is still accurate)

In addition, group by means that only one record is displayed. for example, there may be many records in the database for a certain individual's educational qualifications, however, if the result is displayed in the combined search result on the front end, I need to merge the results and obtain only the highest one.

This article is from the "Golden Pig's PHP growth path" blog

BitsCN.com

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.