我有TABLE ONE和TABLE TWO,两者都有一个名为“n_inscription”的字段
我将这个值保存在两个表中,VARCHAR to a MySQL database
为“n_inscription”LIKE This (NumberOrder/CurrentYear). Example: 1/2013, 2/2013, 13/2014, ....etc
in(表一)
AND TABLE TWO具有这些值
1/2014, 2/2014, 5/2013...
我试过这个QUERY:
$list_students = array();
$sql_students = $mysqli->query("SELECT * FROM `es_student_infos`
WHERE school_year='".$school_year."'
ORDER BY
CAST(RIGHT(n_inscription, 4) AS UNSIGNED) ASC,
CAST(LEFT(n_inscription, LOCATE('/', n_inscription) - 1) AS UNSIGNED) ASC");
while($data1 = $sql_students->fetch_object()){
$list_students[] = $data1;
}
$sql_students2 = $mysqli->query("SELECT * FROM `es_upgrades`
WHERE school_year='".$school_year."'
ORDER BY
CAST(RIGHT(n_inscription, 4) AS UNSIGNED) ASC,
CAST(LEFT(n_inscription, LOCATE('/', n_inscription) - 1) AS UNSIGNED) ASC");
while($data2 = $sql_students2->fetch_object()){
$list_students[] = $data2;
}
//检索数据:
foreach($list_students AS $student):
............
endforeach;
此查询中的问题是它从第一个表中对数据进行排序,然后对第二个表中的数据进行排序,它给出了如下结果:
1/2013| 2/2013 | 13/2014 | 5/2013 | 1/2014 | 2/2014 |
似乎查询开始从表1中排序数据,当它完成数据排序时,它再次开始从TABLE TWO排序数据。
我想同时对两个表中的数据进行排序,给我这样的结果:
二千〇一十三分之一| 2/2013 | 5/2013 | 1/2014 | 2/2014 | 13/2014 |
而不是
二千〇一十三分之一| 2/2013 | 13/2014 | 5/2013 | 1/2014 | 2/2014 |
表格结构:
表一:
`es_student_infos` (
`id_student` int(11) NOT NULL,
`id_grade` int(11) NOT NULL,
`id_class` int(11) NOT NULL,
`n_inscription` varchar(255) NOT NULL,
`date_inscription` date NOT NULL,
`cne` varchar(255) NOT NULL,
`nom_fr` varchar(255) NOT NULL,
`nom_ar` varchar(255) CHARACTER SET utf8 NOT NULL,
`prenom_fr` varchar(255) NOT NULL,
`prenom_ar` varchar(255) CHARACTER SET utf8 NOT NULL,
`date_naissance` date NOT NULL,
`lieu_naissance_ar` varchar(255) CHARACTER SET utf8 NOT NULL,
`lieu_naissance_fr` varchar(255) NOT NULL,
`tel` varchar(255) NOT NULL,
`sexe` varchar(10) CHARACTER SET utf8 NOT NULL,
`ancien_niveau_scolaire` varchar(255) CHARACTER SET utf8 NOT NULL,
`date_ancienNiveauScolaire` varchar(10) NOT NULL,
`ancienne_ecole` varchar(255) CHARACTER SET utf8 NOT NULL,
`type_ecole` varchar(255) CHARACTER SET utf8 NOT NULL,
`type_inscription` varchar(20) CHARACTER SET utf8 NOT NULL,
`assurance` int(11) NOT NULL,
`mensualite` int(11) NOT NULL,
`nom_tuteur` varchar(255) CHARACTER SET utf8 NOT NULL,
`profession_tuteur` varchar(255) CHARACTER SET utf8 NOT NULL,
`adresse_tuteur` varchar(255) CHARACTER SET utf8 NOT NULL,
`tel_tuteur` varchar(255) NOT NULL,
`remarques` text CHARACTER SET utf8 NOT NULL,
`photo` varchar(255) NOT NULL,
`school_year` varchar(10) NOT NULL,
`cours_type` enum('jour','soir') NOT NULL,
`id_subject` int(11) NOT NULL,
`id_prof_soir` varchar(30) NOT NULL,
`montant_soir` varchar(255) NOT NULL,
`archived` enum('0','1') NOT NULL
表二:
`es_upgrades` (
`id_upgrade` int(11) NOT NULL,
`id_student` int(11) NOT NULL,
`n_inscription` varchar(20) NOT NULL,
`old_id_class` int(11) NOT NULL,
`new_id_class` int(11) NOT NULL,
`status` enum('up','down') NOT NULL,
`school_year` varchar(30) NOT NULL,
`date_inscription` date DEFAULT NULL,
`cne` varchar(255) DEFAULT NULL,
`nom_fr` varchar(255) DEFAULT NULL,
`nom_ar` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`prenom_fr` varchar(255) DEFAULT NULL,
`prenom_ar` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`date_naissance` date DEFAULT NULL,
`lieu_naissance_ar` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`lieu_naissance_fr` varchar(255) DEFAULT NULL,
`tel` varchar(255) DEFAULT NULL,
`sexe` varchar(10) CHARACTER SET utf8 DEFAULT NULL,
`ancien_niveau_scolaire` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`date_ancienNiveauScolaire` varchar(10) CHARACTER SET utf8 DEFAULT NULL,
`ancienne_ecole` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`type_ecole` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`type_inscription` varchar(20) CHARACTER SET utf8 DEFAULT NULL,
`assurance` int(11) DEFAULT NULL,
`mensualite` int(11) DEFAULT NULL,
`nom_tuteur` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`profession_tuteur` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`adresse_tuteur` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`tel_tuteur` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`remarques` text CHARACTER SET utf8,
`photo` varchar(255) CHARACTER SET utf8 DEFAULT NULL,
`cours_type` enum('jour','soir') CHARACTER SET utf8 DEFAULT NULL,
`id_subject` int(11) DEFAULT NULL,
`archived` enum('0','1') DEFAULT NULL
答案 0 :(得分:0)
UNION应该可以工作,但它只适用于具有相同列数的表,或者如果您指定了所需的列(在其他单词中,您不能使用*符号)
所以,如果你有像表这样的表:
CREATE TABLE a
(
id SERIAL,
dat DATE,
PRIMARY KEY (id)
);
和
CREATE TABLE b
(
id SERIAL,
dat DATE,
ip VARCHAR(16),
PRIMARY KEY (id)
);
你应该使用这样的请求
SELECT dat FROM a
UNION
SELECT dat FROM b;
然后它应该有用。
阅读本主题(尤其是第二个答案(共7票))它可能会有所帮助:Order by descending date - month, day and year