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.