search
HomeDatabaseMysql Tutorialsqlserver isnull在数据库查询中的应用

isnull在数据库查询中的应用,特别是再语句连接的时候需要用到

isnull在数据库查询中的应用,特别是再语句连接的时候需要用到

比如连接时候,某个字段没有值但是又要左连接到其他表上 就会显示空,

isnull可以判断是否是NULL,如果是给个默认值

isnull("字段名","默认的数据")

SqlServer中的null值与IsNull函数

NULL 值的三大特点,分别是:1)NULL值不参加统计;2)NULL值不进入计算表达式;3)不能与其它值进行比较。

所谓NULL值不参加统计 即 在使用统计函数时,凡是涉及到 NULL值的都会被忽视掉(用词可能不准确),不要以为这不重要,其实在某些地方这是很重要的。而 NULL值不进入计算也就是说在进行数据之间的统计计算时,若有为 NULL值的项,那么它是不进入即不参加计算的。这也是一个不容忽视的问题。这即使在生活中也是常见的。就好比如说一个人的奖金为NULL值(没有录入数据或其它的原因,不做探讨),而他的基本工资总不会为 0 吧?!(如果为0,早就被T了),月末算总工资的时候把基本工资加上奖金,而若是奖金为 NULL值,总工资 = 基本工资 + 奖金(NULL),那么当此种情况发生时,总工资 = 基本工资 ?

肯定的回答是:不等于。因为奖金为 NULL值,NULL值代表具体的什么值?都不知道。而不知道NULL值到底是多少,那么又怎么能进行计算呢?所以总工资等于 NULL 的,也是不确定的。这样,还有员工敢在公司吗?万一你来个总工资的 NULL 为真的空了,那还做什么工作啊?!这就涉及到一个强制转换的问题,即把 NULL值强制转换为 0 ,让其具备业务意义。而强制转换的关键字就是 IS ,语法即 IS NULL;这样就可以进行涉及 NULL值的计算了。

不过 NULL值也不是对所有的统计函数都有影响。一般来说。统计平均值(AVG)时, NULL值是一定会有影响的;统计最小值(MIN)时, NULL值是可能会对 MIN 有影响,在我认为是有点随机性质;统计最大值(MAX)或统计和(NULL)时,NULL值是对其完全没有影响的。

所以又有一种说法是:null值不参加统计,不参加计算,只能用is判断。

判断Null值语句:select * from 表 where 字段 is null;

转换null值语句:select 字段1,字段2,字段3,is null(字段3,'某个值') from 表;

总之,我们要认真对待 NULL值,最好在使用统计函数时,都加上 IS NULL,以防意外出现。

sqlserver 中isnull的用法一例

数据库中有一列记录文章的访问次数。我现在要实现的功能是,每刷新一次页面。 访问次数+1。sql语句,art_count为访问次数,int类型。
update article set art_count="(art_count+1) where art_id="3 但如果art_count为NULL,则不起作用。
如果是oracle用decode可以很容易的实现此功能。sqlserver中如何实现类似的功能呢?
sqlserver中有一个函数isnull,此函数有两个参数isnull(p1,p2)其用法是如果p1为null,则用p2代替。
此函数类似oracle的nvl。例如
SELECT AVG(ISNULL(price, $10.00)) FROM titles 受到此函数的启发我这样写的sql语句
update article set art_count="(isnull(vote_count,0)+1) where art_id="3 "
Statement
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
Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Explain the ACID properties (Atomicity, Consistency, Isolation, Durability).Apr 16, 2025 am 12:20 AM

ACID attributes include atomicity, consistency, isolation and durability, and are the cornerstone of database design. 1. Atomicity ensures that the transaction is either completely successful or completely failed. 2. Consistency ensures that the database remains consistent before and after a transaction. 3. Isolation ensures that transactions do not interfere with each other. 4. Persistence ensures that data is permanently saved after transaction submission.

MySQL: Database Management System vs. Programming LanguageMySQL: Database Management System vs. Programming LanguageApr 16, 2025 am 12:19 AM

MySQL is not only a database management system (DBMS) but also closely related to programming languages. 1) As a DBMS, MySQL is used to store, organize and retrieve data, and optimizing indexes can improve query performance. 2) Combining SQL with programming languages, embedded in Python, using ORM tools such as SQLAlchemy can simplify operations. 3) Performance optimization includes indexing, querying, caching, library and table division and transaction management.

MySQL: Managing Data with SQL CommandsMySQL: Managing Data with SQL CommandsApr 16, 2025 am 12:19 AM

MySQL uses SQL commands to manage data. 1. Basic commands include SELECT, INSERT, UPDATE and DELETE. 2. Advanced usage involves JOIN, subquery and aggregate functions. 3. Common errors include syntax, logic and performance issues. 4. Optimization tips include using indexes, avoiding SELECT* and using LIMIT.

MySQL's Purpose: Storing and Managing Data EffectivelyMySQL's Purpose: Storing and Managing Data EffectivelyApr 16, 2025 am 12:16 AM

MySQL is an efficient relational database management system suitable for storing and managing data. Its advantages include high-performance queries, flexible transaction processing and rich data types. In practical applications, MySQL is often used in e-commerce platforms, social networks and content management systems, but attention should be paid to performance optimization, data security and scalability.

SQL and MySQL: Understanding the RelationshipSQL and MySQL: Understanding the RelationshipApr 16, 2025 am 12:14 AM

The relationship between SQL and MySQL is the relationship between standard languages ​​and specific implementations. 1.SQL is a standard language used to manage and operate relational databases, allowing data addition, deletion, modification and query. 2.MySQL is a specific database management system that uses SQL as its operating language and provides efficient data storage and management.

Explain the role of InnoDB redo logs and undo logs.Explain the role of InnoDB redo logs and undo logs.Apr 15, 2025 am 12:16 AM

InnoDB uses redologs and undologs to ensure data consistency and reliability. 1.redologs record data page modification to ensure crash recovery and transaction persistence. 2.undologs records the original data value and supports transaction rollback and MVCC.

What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?What are the key metrics to look for in an EXPLAIN output (type, key, rows, Extra)?Apr 15, 2025 am 12:15 AM

Key metrics for EXPLAIN commands include type, key, rows, and Extra. 1) The type reflects the access type of the query. The higher the value, the higher the efficiency, such as const is better than ALL. 2) The key displays the index used, and NULL indicates no index. 3) rows estimates the number of scanned rows, affecting query performance. 4) Extra provides additional information, such as Usingfilesort prompts that it needs to be optimized.

What is the Using temporary status in EXPLAIN and how to avoid it?What is the Using temporary status in EXPLAIN and how to avoid it?Apr 15, 2025 am 12:14 AM

Usingtemporary indicates that the need to create temporary tables in MySQL queries, which are commonly found in ORDERBY using DISTINCT, GROUPBY, or non-indexed columns. You can avoid the occurrence of indexes and rewrite queries and improve query performance. Specifically, when Usingtemporary appears in EXPLAIN output, it means that MySQL needs to create temporary tables to handle queries. This usually occurs when: 1) deduplication or grouping when using DISTINCT or GROUPBY; 2) sort when ORDERBY contains non-index columns; 3) use complex subquery or join operations. Optimization methods include: 1) ORDERBY and GROUPB

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
1 months agoBy尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Chat Commands and How to Use Them
1 months agoBy尊渡假赌尊渡假赌尊渡假赌

Hot Tools

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft