COALESCE()函数可以接受一系列的值,如果列表中所有项都为空(null),那么只使用一个值。然后,它将返回第一个非空值。这一技巧描述了创造性使用SQL Server 中COALESCE()函数的两种方法。 这里有一个简单的例子:有一个Persons数据表,它有三个字段FirstNam
COALESCE()函数可以接受一系列的值,如果列表中所有项都为空(null),那么只使用一个值。然后,它将返回第一个非空值。这一技巧描述了创造性使用SQL Server 中COALESCE()函数的两种方法。
这里有一个简单的例子:有一个Persons数据表,它有三个字段FirstName、MiddleName和LastName。表中包含以下值:
John A. MacDonald
Franklin D. Roosevelt
Madonna
Cher
Mary Weilage
如果你想用一个字符串列出他们的全名,下面给出了如何使用COALESCE()函数完成此功能:
SELECT FirstName + '' '' +COALESCE(MiddleName,'''')+ '' '' +COALESCE(LastName,'''')
如果你不想每个查询都这样写,列表A显示了如何将它转换成一个函数。这样当你需要使用这个脚本的时候(不管每个列的实际值是什么),可以直接调用该函数并传递三个字段参数。在下面的例子中,我传递给函数的参数是人名,但是你可以用字段名替代得到同样的结果:
SELECT dbo.WholeName(''James'',NULL,''Bond'')
UNION
SELECT dbo.WholeName(''Cher'',NULL,NULL)
UNION
SELECT dbo.WholeName(''John'',''F.'',''Kennedy'')
测试结果如下:
James Bond
Cher
John F. Kennedy
你可能会注意到我们的一个问题,在James Bond这个名字中有两个空格。通过修改@result这一行可以改正这个问题,如下所示:
SELECT @Result = LTRIM(@first + '' '' + COALESCE(@middle,'''') + '' '') + COALESCE(@last,'''')
下面是COALESCE()函数的另一个应用。在本例中,我们将显示一个支付给员工的工资单。问题是对于不同的员工工资标准是不同的(例如,有些员工是按小时支付,按工作量每周发一次工资或是按责任支付)。列表B中是创建一个样表的代码。下面是一些示例记录,每个是一种类型:
1 18.00 40 NULL NULL NULL NULL
2 NULL NULL 4.00 400 NULL NULL
3 NULL NULL NULL NULL 800.00 NULL
4 NULL NULL NULL NULL 500.00 600
用下面的代码在同一列中列出支付给员工的总额(不管它们的支付标准):
SELECT
EmployeeID,
COALESCE(HourlyWage * HoursPerWeek,0)+
COALESCE(AmountPerPiece * PiecesThisWeek,0)+
COALESCE(WeeklySalary + CommissionThisWeek,0)AS Payment
FROM [Coalesce_Demo].[PayDay]
结果如下:
EmployeeID Payment
1 720.00
2 1600.00
3 800.00
4 1100.00
你可能需要在应用程序中多处使用这一计算方法,虽然这种表示可以完成任务,但是看起来不是很美观。下面列出了如何使用一个单独的求和列来完成这项工作:
ALTERTABLE Coalesce_Demo.PayDay
ADD Payment AS
COALESCE(HourlyWage * HoursPerWeek,0)+
COALESCE(AmountPerPiece * PiecesThisWeek,0)+
COALESCE(WeeklySalary + CommissionThisWeek,0)
这样只要使用SELECT *就可以显示预先计算好的结果。
小结
本文介绍了使用COALESCE()函数一些特殊场合和特殊方式。就我的经验看来,COALESCE()函数最常出现在一个具体的内容中,如一个查询或视图或存储过程中。
你可以将COALESCE()放在一个函数中来使用它,也可以通过将它放在一个单独的计算列中优化性能,并总能获得结果

InnoDBBufferPool reduces disk I/O by caching data and indexing pages, improving database performance. Its working principle includes: 1. Data reading: Read data from BufferPool; 2. Data writing: After modifying the data, write to BufferPool and refresh it to disk regularly; 3. Cache management: Use the LRU algorithm to manage cache pages; 4. Reading mechanism: Load adjacent data pages in advance. By sizing the BufferPool and using multiple instances, database performance can be optimized.

Compared with other programming languages, MySQL is mainly used to store and manage data, while other languages such as Python, Java, and C are used for logical processing and application development. MySQL is known for its high performance, scalability and cross-platform support, suitable for data management needs, while other languages have advantages in their respective fields such as data analytics, enterprise applications, and system programming.

MySQL is worth learning because it is a powerful open source database management system suitable for data storage, management and analysis. 1) MySQL is a relational database that uses SQL to operate data and is suitable for structured data management. 2) The SQL language is the key to interacting with MySQL and supports CRUD operations. 3) The working principle of MySQL includes client/server architecture, storage engine and query optimizer. 4) Basic usage includes creating databases and tables, and advanced usage involves joining tables using JOIN. 5) Common errors include syntax errors and permission issues, and debugging skills include checking syntax and using EXPLAIN commands. 6) Performance optimization involves the use of indexes, optimization of SQL statements and regular maintenance of databases.

MySQL is suitable for beginners to learn database skills. 1. Install MySQL server and client tools. 2. Understand basic SQL queries, such as SELECT. 3. Master data operations: create tables, insert, update, and delete data. 4. Learn advanced skills: subquery and window functions. 5. Debugging and optimization: Check syntax, use indexes, avoid SELECT*, and use LIMIT.

MySQL efficiently manages structured data through table structure and SQL query, and implements inter-table relationships through foreign keys. 1. Define the data format and type when creating a table. 2. Use foreign keys to establish relationships between tables. 3. Improve performance through indexing and query optimization. 4. Regularly backup and monitor databases to ensure data security and performance optimization.

MySQL is an open source relational database management system that is widely used in Web development. Its key features include: 1. Supports multiple storage engines, such as InnoDB and MyISAM, suitable for different scenarios; 2. Provides master-slave replication functions to facilitate load balancing and data backup; 3. Improve query efficiency through query optimization and index use.

SQL is used to interact with MySQL database to realize data addition, deletion, modification, inspection and database design. 1) SQL performs data operations through SELECT, INSERT, UPDATE, DELETE statements; 2) Use CREATE, ALTER, DROP statements for database design and management; 3) Complex queries and data analysis are implemented through SQL to improve business decision-making efficiency.

The basic operations of MySQL include creating databases, tables, and using SQL to perform CRUD operations on data. 1. Create a database: CREATEDATABASEmy_first_db; 2. Create a table: CREATETABLEbooks(idINTAUTO_INCREMENTPRIMARYKEY, titleVARCHAR(100)NOTNULL, authorVARCHAR(100)NOTNULL, published_yearINT); 3. Insert data: INSERTINTObooks(title, author, published_year)VA


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools