📜  MySQL字面量

📅  最后修改于: 2020-11-19 02:19:51             🧑  作者: Mango

字面量(常量)

字面量是表示/表达不变价值的符号或观念。在MySQL中,字面量相似。我们可以在声明变量或执行查询时使用字面量。

在本节中,我们将描述字面量的不同类型以及如何在MySQL语句中使用它们。

以下是字面量的类型:

  • 字符串字面量
  • 数字字面量
  • 日期和时间字面量
  • 十六进制字面量
  • 位值字面量
  • 布尔字面量
  • 空值

字符串字面量

MySQL中的字符串是用单引号(')或双引号(“)引起的字符或字节序列。例如,“第一个字符串”和“第二个字符串”都相同。字节字符串称为一个二进制字符串。每个二进制字符串包含一个字符集和一个排序规则。

让我们借助示例了解它。首先,我们将创建一个名为“ student_info”的表,其中包含以下数据:

如果要获取名为Joseph的学生代码和电话号码,请执行以下语句:

mysql> SELECT stud_code, phone FROM student_info WHERE stud_name = 'Joseph';
OR,
mysql> SELECT stud_code, phone FROM student_info WHERE stud_name = "Joseph";

输出量

它将提供以下输出,我们可以看到两个查询使用单引号或双引号给出相同的结果。

字符串字面量也可以与特殊字符转义序列一起使用。这些特殊字符以表格形式总结如下:

Escape Sequence Character Represented by Sequence
\0 It represents ASCII NULL character.
\b It represents a backspace character.
\n It represents a newline character.
\r It represents carriage return character.
\t It represents tab character.
\\ It represents a backslash (\) character.
\% It represents a % character.
\_ It represents a backslash character.

数字字面量

MySQL中的数字字面量用于指定两种类型的字面量值:精确值(整数和十进制)和近似值(浮点数)字面量。它可以是正值或负值。精确值可以具有整数,分数或两者皆有。近似值主要用于包含尾数和指数的科学计数法。

Number Literals Descriptions
Integer It is represented as a sequence of digits without any fractional parts. If the number preceded by – sign, it is a negative integer. If the number is preceded by + sign, it is a positive integer. If the number does not have any sign, it assumes a positive integer. For example, 55, +55, -55 are all integer numbers.
Decimal It is represented as a sequence of digits with fractional parts. In other words, it contains a whole number plus fractional part, which is separated by dot(.) operator or decimal point. It can be integer and non-integer both. It produces the calculation in exact numeric form. For example, 325.90, 355.6 are decimal numbers.
Floating-Point It is a number that contains a floating decimal point. It means there are no fixed digits before and after the decimal point. It contains two kinds of data types named float and double that produce an approximate value. For example, 2.36E0, 0.005, and -2,328.679 are all floating-point numbers.

如果我们要获取学生的姓名,科目和成绩> +80的成绩,请执行以下语句:

mysql> SELECT stud_name, subject, marks FROM student_info WHERE marks > +80;

输出量

它将提供以下输出,在这里我们可以看到所有分数> +80的学生姓名。

日期和时间字面量

MySQL中的日期和时间值可以用带引号的字符串或数字格式表示,这取决于确切的值和某些因素。例如,MySQL将此'2020-09-22','20200922'和20200922中的任何一个解释为有效日期。

下表说明了MySQL中日期值的格式:

Date Format Descriptions
‘YYYY-MM-DD’ or ‘YY-MM-DD’ It represents a date in string format with punctuations that can be used as a delimiter between the date parts. For example, ‘2020-03-31’, ‘2020/03/31’, and ‘2020^03^31’ all are same date values.
‘YYYYMMDD’ or ‘YYMMDD’ It represents a date in the string format without any punctuations or delimiter between the date parts. For example, ‘20200422 and ‘200522’ are interpreted as ‘2007-05-23’, but ‘071342’ is illegal and becomes ‘0000-00-00’ date value.
YYYYMMDD or YYMMDD It represents a date in the number format. For example, 20200305 and 200305 are interpreted as ‘2020-03-05’ date value.

下表说明了MySQL中时间值的格式:

Time Format Descriptions
‘D hh:mm:ss’, ‘hh:mm:ss’, ‘hh:mm’, ‘D hh:mm’, ‘D hh’, or ‘ss’ It represents a time in string format with punctuations that can be used as a delimiter between the time parts. Here, D for days that have value from 0 to 34. For example, ’22 10:11:12′, ’10:11:12′ time value.
‘hhmmss’ It represents a time in the string format without any punctuations or delimiter between the time parts. For example, ‘101211’ is interpreted as ’10:12:11′, but ‘109813’ is illegal and becomes ’00:00:00′ time value.
hhmmss, ss, or mmss, It represents a time in the number format. For example, 101211 is interpreted as ’10:12:11′ time value.

下表说明了MySQL中datetime和timestamp值的格式:

DateTime Format Descriptions
‘YYYY-MM-DD hh:mm:ss’ or ‘YY-MM-DD hh:mm:ss’ It represents a date and time in string format with punctuations that can be used as a delimiter between the date and time parts. For example, ‘2020-05-31 12:30:45’, ‘2020/05/31 12*30*45’, and ‘2020@05@31 12^30^45’ all are same values.
‘YYYYMMDDhhmmss’ or ‘YYMMDDhhmmss’ It represents a date in the string format without any punctuations or delimiter between the date and time parts. For example, ‘20070523091528’ and ‘070523091528’ are interpreted as ‘2007-05-23 09:15:28’, but ‘071122129015’ is illegal and becomes ‘0000-00-00 00:00:00’ date and time value.
YYYYMMDDhhmmss or YYMMDDhhmmss It represents a date and time in the number format. For example, 20200105142500 and 200105142500 are interpreted as ‘2020-01-05 14:25:00’ date and time value.

例子1

假设我们有一个名为“ orders”的表,其中包含以下数据:

如果我们要获取不同日期格式的Order_ID,Product_Name,请执行以下语句:

mysql> SELECT Order_ID, Product_Name, DATE_FORMAT(Order_Date,'%d--%m--%y') as new_date_formate FROM orders;

输出量

它将提供以下输出,在这里我们可以看到日期的格式将被更改。

例子2

mysql> SELECT Order_ID, Product_Name, DATE_FORMAT(Order_Date,'%d%m%y 11:30:45') as new_date_formate FROM orders;

输出量

十六进制字面量

在编号系统中,十六进制可以表示为一个以16为底的整数。十六进制字面量值可以用以下术语表示:

  • x'val'
  • X'val'
  • 0xval

此处,val包含(0..9和A..F)范围内的十六进制数字。在0xval中,前导0x区分大小写;因此我们不能将其写为0X'val'。但是,对于数字的字母大小写,对于特殊的区分大小写,前导X或0x无关紧要。

以下示例说明了合法和非法的十六进制字面量:

Legal Illegal
x’01BF’
x’01bf’
X’01BF’
X’01bf’
0x01BF
0x01bf
X’0H’ (Because H does not a hexadecimal digit)
0X0BAF (Because 0X should be written as 0x)

它确保符号X'val'或x'val'应该包含偶数个数字。否则,我们将收到语法错误。可以通过在字符串的开头填充零数字来避免这种类型的错误。

以下示例对其进行了更清晰的说明:

mysql> SELECT 0xD6+0;
mysql> SELECT HEX('javatpoint');

输出量

布尔字面量

MySQL中的布尔字面量总是以1或0值求值。此处,1代表真,0代表假常数。

让我们用下面的例子来理解它:

mysql> SELECT TRUE, true, FALSE, false;

输出量

位值字面量

MySQL中的位值字面量可以用b'val'或0bval表示法表示。此处,val是包含零和一的二进制值,并且b的任何前导值的字母大小写都无关紧要。前导0b值区分大小写,因此我们不能将其写为0B。

以下示例说明了合法和非法的位值字面量:

Legal Illegal
b’0011′
B’0011′
0b0011
b’3′ (3 is not a binary digit)
0B11 (0B should be written as 0b)

下面的示例有助于清楚地理解它:

mysql> SET @v1 = b'1100011', @v2 = b'1100001'+0, @v3 = CAST(b'1100001' AS UNSIGNED);

输出量

空值

它代表没有数据。它不区分大小写,这意味着我们可以用任何语言编写空字面量。