case实例
SQL中的CASE WHEN语句是经常要用到的,下面将结合实例,为您详解CASE WHEN语句的使用,供您参考,希望对您学习SQL语句能有所帮助。 一张表数据如下 1900-1-1 胜 1900-1-1 胜 1900-1-1 负 1900-1-2 胜 1900-1-2 胜 写出一条SQL语句,使检索结果如下: 胜 负 19
SQL中的CASE WHEN语句是经常要用到的,下面将结合实例,为您详解CASE WHEN语句的使用,供您参考,希望对您学习SQL语句能有所帮助。
一张表数据如下1900-1-1 胜
1900-1-1 胜
1900-1-1 负
1900-1-2 胜
1900-1-2 胜
写出一条SQL语句,使检索结果如下:
胜 负
1900-1-1 2 1
1900-1-2 2 0
我随手建了这样一个表:
50) null)
并将上面的数据都插入到表中。
经过一番尝试和修改,终于得到了答案:
select distinct Date, Result 1 , Result 1 '负' from test group by date
这里我要说的,其实是SQL中case when的用法。它在普通的SQL语句中似乎并不常见,我本人以前也没在实际项目中使用过。遇到类似问题,往往通过代码或多条SQL语句实现。或者是如下这种丑陋的SQL,并且还伴随着很多潜在的BUG(如,当没有‘负’时)。
select a.date,a.a1 胜,b.b1 负 from datedate) a, (datedate) b where a.date=b.date
我们不妨来复习一下CASE WHEN的语法。
CASE WHEN有两种用法,一种是类似上面例子中那样的简单CASE函数:
CASE result 1 2 ELSE 0 END
还有一种是CASE搜索函数:
1 2 ELSE 0 END
其中result='胜'可以替换为其他条件表达式。如果有多个CASE WHEN表达式符合条件,美国服务器,将只返回第一个符合条件的子句,网站空间,香港虚拟主机,其余子句将被忽略。
用CASE WHEN语句可以简化我们平时工作中遇到的很多问题。如性别在表中存的是数字1、2,但是希望查询出来男、女时,可以这样:

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

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

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

Hot Topics





Support Vector Machine (SVM) in Python is a powerful supervised learning algorithm that can be used to solve classification and regression problems. SVM performs well when dealing with high-dimensional data and non-linear problems, and is widely used in data mining, image classification, text classification, bioinformatics and other fields. In this article, we will introduce an example of using SVM for classification in Python. We will use the SVM model from the scikit-learn library

Golang is a powerful and efficient programming language that can be used to develop various applications and services. In Golang, pointers are a very important concept, which can help us operate data more flexibly and efficiently. Pointer conversion refers to the process of pointer operations between different types. This article will use specific examples to learn the best practices of pointer conversion in Golang. 1. Basic concepts In Golang, each variable has an address, and the address is the location of the variable in memory.

As the new generation of front-end frameworks continues to emerge, VUE3 is loved as a fast, flexible, and easy-to-use front-end framework. Next, let's learn the basics of VUE3 and make a simple video player. 1. Install VUE3 First, we need to install VUE3 locally. Open the command line tool and execute the following command: npminstallvue@next Then, create a new HTML file and introduce VUE3: <!doctypehtml>

VAE is a generative model, its full name is VariationalAutoencoder, which is translated into Chinese as variational autoencoder. It is an unsupervised learning algorithm that can be used to generate new data, such as images, audio, text, etc. Compared with ordinary autoencoders, VAEs are more flexible and powerful and can generate more complex and realistic data. Python is one of the most widely used programming languages and one of the main tools for deep learning. In Python, there are many excellent machine learning and deep

With the rapid development of the Internet, data has become one of the most important resources in today's information age. As a technology that automatically obtains and processes network data, web crawlers are attracting more and more attention and application. This article will introduce how to use PHP to develop a simple web crawler and realize the function of automatically obtaining network data. 1. Overview of Web Crawler Web crawler is a technology that automatically obtains and processes network resources. Its main working process is to simulate browser behavior, automatically access specified URL addresses and extract all information.

When the HMD Skyline(available on Amazon for $499) was launched last month, it was released in two colors - Neon Pink and Twisted Black. They are now joined by a third color dubbed Blue Topaz. HMD Global has also announced an official case for the ph

The relationship between the number of Oracle instances and database performance Oracle database is one of the well-known relational database management systems in the industry and is widely used in enterprise-level data storage and management. In Oracle database, instance is a very important concept. Instance refers to the running environment of Oracle database in memory. Each instance has an independent memory structure and background process, which is used to process user requests and manage database operations. The number of instances has an important impact on the performance and stability of Oracle database.

With the popularity of the Internet, verification codes have become a necessary process for login, registration, password retrieval and other operations. In the Gin framework, implementing the verification code function has become extremely simple. This article will introduce how to use a third-party library to implement the verification code function in the Gin framework, and provide sample code for readers' reference. 1. Install dependent libraries Before using the verification code, we need to install a third-party library goCaptcha. To install goCaptcha, you can use the goget command: $goget-ugithub
