Questions about SQL concatenation?

Source: Internet
Author: User
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
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.