PHP query MySQL, according to the ID field, sorted by birth date

Source: Internet
Author: User
My MySQL data sheet person has an ID number field, which I want to sort by the date of birth, which is the seventh to 14th field in the ID number. The focus is on sorting. The CREATE statement for the person table is as follows:
$persql = "CREATE TABLE person
(
ID int not NULL auto_increment,
PersonID char () not NULL,
PersonName varchar (20),
Homeid int,
About varchar (10),
Sex varchar (4),
edu varchar (8),
MZ varchar (4),
PRIMARY KEY (ID),
UNIQUE (PersonID)
) Engine=innodb DEFAULT Charset=utf8 ";
Query Statement $sql= "SELECT * from person";
$sp =mysql_query ($sql, $con);
Where the PersonID field stores the ID number. I'm going to output all the fields as shown,
Want to sort by the date of birth.
The way I think about it now is to store the data in a two-dimensional array.
$arr =array ();
$i = 0;
while ($row = Mysql_fetch_array ($sp)) {

$arr [$i]= $row;
$i + +;
}
The two-dimensional array is sorted and then output. But I think this method is too troublesome, I hope there is a better way.


Reply to discussion (solution)

It can be sorted in MySQL.
SELECT * FROM Person ORDER by SUBSTRING (personid,7,8) desc

SELECT * FROM Person ORDER by SUBSTRING (personid,6,13) desc
Thanks for upstairs! This function is very useful, and get to a new skill.

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