Question about SQL concatenation ?? Requirement: Sort By province, customer name, and ventilation time. you can select only one or more conditions. The three sorting conditions are check boxes, and SQL & nbsp; is as follows: $ MYSQL-& gt; query ("select & nbsp; * & nbsp; from & nbs find an SQL concatenation question ??
Requirement: Sort By province, customer name, and ventilation time. you can select only one or more conditions. The three sorting conditions are check boxes,
The SQL statement is as follows:
$ MYSQL-> query ("select * from customer cu". $ where );
Sorting condition:
Province
Customer name
Ventilation time
Sorting conditions after SQL
If ($ sel_key10 ){
$ Where = $ where. "order by cu. province"; // province
}
If ($ sel_key11 ){
$ Where. = ", cu. KFName"; // customer name
}
If ($ sel_key12 ){
$ Where. = ", cu. GiveGasTime"; // ventilation time
}
There is a problem with splicing like this. if the province is used as the sorting condition, it is okay, but I use other as the sorting condition separately. if I use two at the same time, or three, according to, province, customer name, ventilation time, this order is also correct,
In fact, the SQL I want is:
Select * from customer cu where 1 = 1 order by cu. province, cu. KFName, cu. GiveGasTime. If no conditions exist, do not add order by. If yes, append it in brackets ..
Sort condition: province customer name ventilation time
SQL concatenation question ?? Shared to: query ("select? *? From... 'data-pics = ''>
------ Solution --------------------
It can be written in this way.
$ Where = array ();
If ($ sel_key10 ){
$ Where [] = "cu. province"; // province
}
If ($ sel_key11 ){
$ Where [] = "cu. KFName"; // customer name
}
If ($ sel_key12 ){
$ Where [] = "cu. GiveGasTime"; // ventilation time
}
$ MYSQL-> query ("select * from customer cu ".(! Empty ($ where )? 'Order by '. implode (', ', $ where ):'');
------ Solution --------------------
Test Code
$ Dict = array (
'Sel _ key10' => 'CU. province ', // province
'Sel _ key11' => 'CU. kfname', // customer name
'Sel _ key12' => 'CU. GiveGasTime ', // ventilation time