博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
SQL查询初学者指南读书笔记(三)值表达式
阅读量:4121 次
发布时间:2019-05-25

本文共 2368 字,大约阅读时间需要 7 分钟。

CHAPTER 5 Getting More Than Simple Columns

Intro Value  expression,it contains column names, literal values, functions, or other value

expressions

 

The type of data

The SQL Standard defines seven general categories of types of data—character,

national character, binary, numeric, Boolean, datetime, and interval.

 

The national character data type is the same as the character data type except that it draws its characters from ISO-defined foreign language character sets.

 

BOOLEAN数据类型可以使用TINYINT存储.

 

CAST

Changing Data Types

 

data_type需要查看具体数据库实现文档,比如MySQL

The type for the result can be one of the following values:

  • BINARY[(N)]
  • CHAR[(N)]
  • DECIMAL[(M[,D])]
  • SIGNED [INTEGER]
  • UNSIGNED [INTEGER]

 

源文档 <>

 

比如

SELECT OrderNumber, OrderDate, ShipDate,

CAST(ShipDate– OrderDate AS INTEGER)

AS DaysElapsed

FROM Orders

 

MySQL中需要转为

 

SELECT OrderNumber, OrderDate, ShipDate,

CAST(ShipDate- OrderDate AS DECIMAL)

AS DaysElapsed

FROM Orders

 

Literal Value

分为字符串常量值,数值常量值和日期常量值.

 

如果使用单引号包含字符串,字符串中包含单引号,字符串中的单引号请使用两次以示与引用的单引号区别开来.

SQL 'The Vendor"s name is: '

Displayed as The Vendor's name is:

 

Types of Expressions

 

 

CONCATENATION

SQL字符串串接

 

Note Of the major database systems,we found that only IBM’s DB2 and

Informix and Oracle’s Oracle support the SQL Standard operator for concatenation.

Microsoft Office Access supports & and + as concatenation

operators,Microsoft SQL Server and Ingres support +, and in MySQL you

must use the CONCAT function. In all the examples in the book,we use the

SQL Standard || operator. In the sample databases on the CD,we use the

appropriate operator for each database type (Microsoft Access, Microsoft

SQLServer, and MySQL).

 

由上图可知SQL标准字符串串接用||,但是在MySQL使用会被当作逻辑运算符。MySQL使用concat内建函数串接字符串。而有的数据库使用直观的+串接字符串.

 

DATE AND TIME ARITHMETIC

MySQL需要将日期常量值转为相应类型再做计算,另外需要使用相应的日期函数计算

CAST('2016-11-22'AS DATE)

CAST('03:30:25'AS TIME)

CAST('2008-09-2914:25:00' AS DATETIME)

 

比如

SELECT StfLastName || ', ' || StfFirstName AS Staff,

DateHired,

CAST(CAST('2007-10-01'- DateHired AS INTEGER)

/365 AS INTEGER)

AS YearsWithSchool

FROM Staff

ORDER BY StfLastName, StfFirstName

 

MySQL中转为

 

SELECT CONCAT(StfLastName , ', ' , StfFirstName) AS Staff,

DateHired,

CAST(DATEDIFF(CAST('1990-01-11'AS date) , DateHired )/365 as decimal)

AS YearsWithSchool

FROM Staff

ORDER BY StfLastName, StfFirstName

 

 

That"Nothing" Value:Null

判断Value Expression是否为NULL的时候请不要使用ValueExpression = NULL,这是常犯的小错误.

你可能感兴趣的文章
C#入门
查看>>
C#中ColorDialog需点两次确定才会退出的问题
查看>>
数据库
查看>>
nginx反代 499 502 bad gateway 和timeout
查看>>
linux虚拟机安装tar.gz版jdk步骤详解
查看>>
python猜拳游戏
查看>>
python实现100以内自然数之和,偶数之和
查看>>
python数字逆序输出及多个print输出在同一行
查看>>
ESP8266 WIFI数传 Pixhaw折腾笔记
查看>>
苏宁产品经理面经
查看>>
百度产品经理群面
查看>>
去哪儿一面+平安科技二面+hr面+贝贝一面+二面产品面经
查看>>
element ui 弹窗在IE11中关闭时闪现问题修复
查看>>
vue 遍历对象并动态绑定在下拉列表中
查看>>
Vue动态生成el-checkbox点击无法选中的解决方法
查看>>
python __future__
查看>>
MySQL Tricks1
查看>>
python 变量作用域问题(经典坑)
查看>>
pytorch
查看>>
pytorch(二)
查看>>