第一部分 计算和别名
1.1 计算字段
之前我们的操作都是直接筛选,那么计算字段考虑到了其他的可能,比如
1. 选择特定的单词或数值;
2. 对单个或多个列进行计算;
3. 把列和直接量组合在一起.
1.2直接量
MariaDB [study_db]> SELECT 'BOOK_NAME: ', book_name FROM books; +-------------+------------------+ | BOOK_NAME: | book_name | +-------------+------------------+ | BOOK_NAME: | 跟我学MariaDB | | BOOK_NAME: | 跟我学NodeJS | | BOOK_NAME: | HTTP权威指南 | |
1.3 算术运算
列直接的操作: + - * /等;直接上例子,两个列相乘,
MariaDB [study_db]> SELECT classify, book_price, 'result: ', classify * book_price from books; +----------+------------+----------+-----------------------+ | classify | book_price | result: | classify * book_price | +----------+------------+----------+-----------------------+ | 18 | 44.00 | result: | 792 | | 18 | 44.44 | result: | 799.92 | | 18 | 79.00 | result: | 1422 | | 9 | 26.00 | result: | 234 | |
1.4 连接字段
连接(CONCAT)就是将不同的列组合在一起;
MariaDB [study_db]> SELECT book_author_name, book_name, 'RESULT:', CONCAT(book_author_name,'出的� 是: ', book_name) FROM books; +------------------+------------------+---------+-----------------------------------------------------------+ | book_author_name | book_name | RESULT:| CONCAT(book_author_name,'出的� 是: ', book_name) | +------------------+------------------+---------+-----------------------------------------------------------+ | hhw | 跟我学MariaDB | RESULT: | hhw出的� 是: 跟我学MariaDB | | hhw | 跟我学NodeJS | RESULT: | hhw出的� 是: 跟我学NodeJS | | O'Reilly | HTTP权威指南 | RESULT: | O'Reilly出的� 是: HTTP权威指南 | |
1.5 列/表的别名--AS
AS关键词, 将旧列以新列名显示;
MariaDB [study_db]> SELECT book_id , book_id AS 'id' FROM books; +---------+----+ | book_id | id | +---------+----+ | 1 | 1 | | 2 | 2 | | 3 | 3 | |