PHP queries mysql, which is sorted by birth year, month, and day based on the ID card field

Source: Internet
Author: User
PHP queries mysql. according to the ID card field, sort by date of birth. my mysql data table "person" contains an ID card number field, that is, sort by the seventh to 14th fields in the ID card number. The focus is sorting. The statement for creating the person table is as follows:
$ Persql = "create table person
(
ID int not null AUTO_INCREMENT,
PersonID char (18) 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 );
The personID field stores the ID number. I want to output all fields, as shown in,
It is expected to be sorted by the date of birth.
Currently, I am thinking of storing data in a two-dimensional array.
$ Arr = array ();
$ I = 0;
While ($ row = mysql_fetch_array ($ sp )){

$ Arr [$ I] = $ row;
$ I ++;
}
Sort two-dimensional arrays and then output them. However, I think this method is too troublesome and I hope there will be better methods.


Reply to discussion (solution)

The sorting can be performed in mysql.
SELECT * FROM person order by SUBSTRING (personID, 7,8) desc

SELECT * FROM person order by SUBSTRING (personID, 6, 13) desc
Thank you! This function is too easy to use and get a new skill.

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.