Home Backend Development PHP Tutorial Dynamic sorting of database query results (1)_PHP tutorial

Dynamic sorting of database query results (1)_PHP tutorial

Jul 13, 2016 pm 05:01 PM
Appear dynamic exist how talent sort database Inquire root of result

A frequently asked question in public newsgroups is "How can I return a sorted output based on the parameters passed to a stored procedure?". With the help of some high-level experts, I've put together several solutions to this problem.
1. Use IF...ELSE to execute pre-written queries
For most people, the first thing that comes to mind may be: execute several pre-written queries through IF...ELSE statements one of the. For example, suppose you want to query the Northwind database to get a sorted list of shippers. The calling code specifies a column in the form of a stored procedure parameter, and the stored procedure sorts the output results based on this column. Listing 1 shows one possible implementation of such a stored procedure (GetSortedShippers stored procedure).
[Listing 1: Execute one of multiple pre-written queries with IF...ELSE]
CREATE PROC GetSortedShippers
@OrdSeq AS int
AS
IF @OrdSeq = 1
SELECT * FROM Shippers ORDER BY ShipperID
ELSE IF @OrdSeq = 2
SELECT * FROM Shippers ORDER BY CompanyName
ELSE IF @OrdSeq = 3
SELECT * FROM Shippers ORDER BY Phone

The advantage of this method is that the code is very simple and easy to understand. The query optimizer of SQL Server can create a query optimization plan for each SELECT query to ensure that the code has optimal performance. The main disadvantage of this approach is that if the query requirements change, you must modify multiple independent SELECT queries - in this case, three.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/631158.htmlTechArticleIn public newsgroups, a frequently asked question is "How can I return a parameter based on the parameters passed to the stored procedure? Sorted output?" With the help of some high-level experts,...
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot Article Tags

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to check your academic qualifications on Xuexin.com How to check your academic qualifications on Xuexin.com Mar 28, 2024 pm 04:31 PM

How to check your academic qualifications on Xuexin.com

12306 How to check historical ticket purchase records How to check historical ticket purchase records 12306 How to check historical ticket purchase records How to check historical ticket purchase records Mar 28, 2024 pm 03:11 PM

12306 How to check historical ticket purchase records How to check historical ticket purchase records

How does Go language implement the addition, deletion, modification and query operations of the database? How does Go language implement the addition, deletion, modification and query operations of the database? Mar 27, 2024 pm 09:39 PM

How does Go language implement the addition, deletion, modification and query operations of the database?

Detailed tutorial on establishing a database connection using MySQLi in PHP Detailed tutorial on establishing a database connection using MySQLi in PHP Jun 04, 2024 pm 01:42 PM

Detailed tutorial on establishing a database connection using MySQLi in PHP

How does Hibernate implement polymorphic mapping? How does Hibernate implement polymorphic mapping? Apr 17, 2024 pm 12:09 PM

How does Hibernate implement polymorphic mapping?

Advanced sorting of PHP arrays: custom comparators and anonymous functions Advanced sorting of PHP arrays: custom comparators and anonymous functions Apr 27, 2024 am 11:09 AM

Advanced sorting of PHP arrays: custom comparators and anonymous functions

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos Jul 18, 2024 am 05:48 AM

iOS 18 adds a new 'Recovered' album function to retrieve lost or damaged photos

An in-depth analysis of how HTML reads the database An in-depth analysis of how HTML reads the database Apr 09, 2024 pm 12:36 PM

An in-depth analysis of how HTML reads the database

See all articles