MySQL学习足迹记录08--创建计算字段--Concat(),AS_MySQL
bitsCN.com
MySQL学习足迹记录08--创建计算字段--Concat(),AS
本文所用到的表格数据
mysql> SELECT * FROM vendors;+---------+----------------+-----------------+-------------+------------+----------+--------------------------------------------+| vend_id | vend_name | vend_address | vend_city | vend_state | vend_zip | vend_country |+---------+----------------+-----------------+-------------+------------+----------+---------------------------------------------+| 1001 | Anvils R Us | 123 Main Street | Southfield | MI | 48075 | USA || 1002 | LT Supplies | 500 Park Street | Anytown | OH | 44333 | USA || 1003 | ACME | 555 High Street | Los Angeles | CA | 90046 | USA || 1004 | Furball Inc. | 1000 5th Avenue | New York | NY | 11111 | USA || 1005 | Jet Set | 42 Galaxy Road | London | NULL | N16 6PS | England || 1006 | Jouets Et Ours | 1 Rue Amusement | Paris | NULL | 45678 | France |+---------+----------------+-----------------+-------------+------------+----------+-----------------------------------------------+6 rows in set (0.00 sec)
1.计算字段并不实际存在于数据库表中,计算字段是在运行时在SELECT语句内创建的。
2.拼接字段Concat()
*拼接:将值连结到一起构成单个值
拼接两个列
eg: mysql> SELECT Concat(vend_name,'(',vend_country,')') FROM vendors -> ORDER BY vend_name;+-------------------------------------------------------+| Concat(vend_name,'(',vend_country,')') |+--------------------------------------------------------+| ACME(USA) || Anvils R Us(USA) || Furball Inc.(USA) || Jet Set(England) || Jouets Et Ours(France) || LT Supplies(USA) |+---------------------------------------------------------+6 rows in set (0.00 sec)
3.删除右侧多余的空格RTrim()
删除左侧多余的空格LTrim()
删除两侧多余的空格Trim()
以RTrim()为例:
eg: mysql> SELECT Concat(RTrim(vend_name),'(',RTrim(vend_country),')') -> FROM vendors -> ORDER BY vend_name;+------------------------------------------------------+| Concat(RTrim(vend_name),'(',RTrim(vend_country),')') |+------------------------------------------------------+| ACME(USA) || Anvils R Us(USA) || Furball Inc.(USA) || Jet Set(England) || Jouets Et Ours(France) || LT Supplies(USA) |+------------------------------------------------------+6 rows in set (0.00 sec)
4.使用别名(alias),关键字AS
eg: mysql> SELECT Concat(Trim(vend_name),'(',Trim(vend_country),')') -> AS vend_title -> FROM vendors -> ORDER BY vend_name;+------------------------+| vend_title |+------------------------+| ACME(USA) || Anvils R Us(USA) || Furball Inc.(USA) || Jet Set(England) || Jouets Et Ours(France) || LT Supplies(USA) |+------------------------+6 rows in set (0.00 sec)
5.执行算术计算
先检索订单号20005中的所有物品
eg: mysql> SELECT prod_id,quantity,item_price -> FROM orderitems -> WHERE order_num = 20005;+---------+----------+------------+| prod_id | quantity | item_price |+---------+----------+------------+| ANV01 | 10 | 5.99 || ANV02 | 3 | 9.99 || TNT2 | 5 | 10.00 || FB | 1 | 10.00 |+---------+----------+------------+4 rows in set (0.00 sec)
再汇总物品的价格(单价*数量)
eg: mysql> SELECT prod_id,quantity,item_price, -> quantity*item_price AS expanded_price -> FROM orderitems -> WHERE order_num = 20005;+---------+----------+------------+----------------+| prod_id | quantity | item_price | expanded_price |+---------+----------+------------+----------------+| ANV01 | 10 | 5.99 | 59.90 || ANV02 | 3 | 9.99 | 29.97 || TNT2 | 5 | 10.00 | 50.00 || FB | 1 | 10.00 | 10.00 |+---------+----------+------------+----------------+4 rows in set (0.00 sec)
6.MySQL支持的基本算术操作符+,-,*,/
bitsCN.com
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

AI Hentai Generator
Generate AI Hentai for free.

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

Big data structure processing skills: Chunking: Break down the data set and process it in chunks to reduce memory consumption. Generator: Generate data items one by one without loading the entire data set, suitable for unlimited data sets. Streaming: Read files or query results line by line, suitable for large files or remote data. External storage: For very large data sets, store the data in a database or NoSQL.

MySQL query performance can be optimized by building indexes that reduce lookup time from linear complexity to logarithmic complexity. Use PreparedStatements to prevent SQL injection and improve query performance. Limit query results and reduce the amount of data processed by the server. Optimize join queries, including using appropriate join types, creating indexes, and considering using subqueries. Analyze queries to identify bottlenecks; use caching to reduce database load; optimize PHP code to minimize overhead.

Backing up and restoring a MySQL database in PHP can be achieved by following these steps: Back up the database: Use the mysqldump command to dump the database into a SQL file. Restore database: Use the mysql command to restore the database from SQL files.

How to insert data into MySQL table? Connect to the database: Use mysqli to establish a connection to the database. Prepare the SQL query: Write an INSERT statement to specify the columns and values to be inserted. Execute query: Use the query() method to execute the insertion query. If successful, a confirmation message will be output.

One of the major changes introduced in MySQL 8.4 (the latest LTS release as of 2024) is that the "MySQL Native Password" plugin is no longer enabled by default. Further, MySQL 9.0 removes this plugin completely. This change affects PHP and other app

To use MySQL stored procedures in PHP: Use PDO or the MySQLi extension to connect to a MySQL database. Prepare the statement to call the stored procedure. Execute the stored procedure. Process the result set (if the stored procedure returns results). Close the database connection.

Creating a MySQL table using PHP requires the following steps: Connect to the database. Create the database if it does not exist. Select a database. Create table. Execute the query. Close the connection.

Oracle database and MySQL are both databases based on the relational model, but Oracle is superior in terms of compatibility, scalability, data types and security; while MySQL focuses on speed and flexibility and is more suitable for small to medium-sized data sets. . ① Oracle provides a wide range of data types, ② provides advanced security features, ③ is suitable for enterprise-level applications; ① MySQL supports NoSQL data types, ② has fewer security measures, and ③ is suitable for small to medium-sized applications.
