Home Database Mysql Tutorial 如何用MySQL内置函数来转换ip地址与相关数字

如何用MySQL内置函数来转换ip地址与相关数字

Jun 07, 2016 pm 04:14 PM
mysql built-in function address number Related Convert

以下的文章主要介绍的是如何用MySQL内置函数来转换ip地址与相关数字,在实际操作中在处理字符格式的时候,例如将54789转换为5,4789这样的,我们只要用:format(12345,0)即可,以下就是具体内容介绍。 用MySQL内置函数转换ip地址和数字 利用两个内置函数 inet_at

以下的文章主要介绍的是如何用MySQL内置函数来转换ip地址与相关数字,在实际操作中在处理字符格式的时候,例如将54789转换为5,4789这样的,我们只要用:format(12345,0)即可,以下就是具体内容介绍。

用MySQL内置函数转换ip地址和数字

利用两个内置函数

inet_aton:将ip地址转换成数字型

inet_ntoa:将数字型转换成ip地址

充分利用mysql内置的format函数

尤其是在处理字符格式的时候,例如将12345转换成12,345这样的,只要用:format(12345,0)即可,如果用format(12345,2)则显示的是12,345.00了...

利用MySQL内置函数处理时间戳问题

<ol class="dp-xml"><li class="alt"><span><span>eg : select FROM_UnixTIME(UNIX_TIMESTAMP(),'%Y %D %M %h:%i:%s %x'); </span></span></li></ol>
Copy after login

结果: 2004 3rd August 03:35:48 2004

利用mysql_convert_table_format转换表类型

需要DBI和DBD的mysql相关模块支持才能用,例子:

<ol class="dp-xml"><li class="alt"><span><span>mysql_convert_table_format </span><span class="attribute">--user</span><span>=</span><span class="attribute-value">root</span><span> </span><span class="attribute">--password</span><span>=</span><span class="attribute-value">'xx'</span><span> </span><span class="attribute">--type</span><span>=</span><span class="attribute-value">myisam</span><span> test yejr </span></span></li></ol>
Copy after login

修改mysql表中的字段名

<ol class="dp-xml"><li class="alt"><span><span>alter table tb_name change old_col new_col definition... </span></span></li></ol>
Copy after login

利用临时变量

<ol class="dp-xml"><li class="alt"><span><span>select @</span><span class="attribute">var1:</span><span>=</span><span class="attribute-value">a1</span><span>+a2 as a_sum,@</span><span class="attribute">var2:</span><span>=</span><span class="attribute-value">b1</span><span>+b2 as b_sum,@var1+@var2 as total_sum from test_table xxx; </span></span></li></ol>
Copy after login

用int类型存储ip地址

原先错误的认为必须用bigint才够,后来发现使用int unsigned类型就足够了。 :)

利用IF函数快速修改ENUM字段值

一个例子:

<ol class="dp-xml"><li class="alt"><span><span>update rule set </span><span class="attribute">enable</span><span> = </span><span class="attribute-value">if</span><span>('0' = enable,'1','0') where xxx; </span></span></li></ol>
Copy after login

enable 类型:enum('0','1') not null default '0'

事务无法嵌套

避免长时间的sleep连接造成的连接数超出问题

设定全局变量 wait_timeout 和 interactive_timeout 为比较小的值,例如

10(s),就能使每个sleep连接在10s之后如果还没有查询的话自动断开。

设定mysql内置函数客户端的提示符(prompt)

<ol class="dp-xml"><li class="alt"><span><span>export </span><span class="attribute">MYSQL_PS1</span><span>=</span><span class="attribute-value">"(\u:\h:)\d> "</span><span> </span></span></li></ol>
Copy after login

则用 mysql -hlocalhost -uroot -pxx db_name 登录后,提示符变成:

<ol class="dp-xml"><li class="alt"><span><span>(root:localhost:)db_name</span><span class="tag">></span><span> </span></span></li></ol>
Copy after login

好用吧 :), 时时刻刻提醒你在哪个服务器上,尽量避免误操作的发生

更详细的请查看MySQL手册

整理MyISAM碎片

1.) 定期运行 OPTIMIZE TABLE 命令即可

2.) 用mysqldump出数据,然后重新import回去,这对大表来说显然第一种方法比较方便

整理Innodb碎片

1.) 运行NULL命令, ALTER TABLE XXX ENGINE=Innodb;

2.) 同上的mysqldump方法

MySQL如果认为检索的记录数量超过总记录数的30%,则选择全表扫描,而非使用索引

MySQL 5.0.3之后,VARCHAR字段后面的空格就不再删除

升级到4.1.1或更高后,就很难降级回到4.0或4.1了,因为 InnoDB 使用了多个表空间的缘故

MySQL 4.1之后,MySQL把字符串类型字段的长度定义理解为字符长度而不是字节长度

MySQL 4.1=>5.0时,增加了一个新的启动选项 innodb_table_locks,它导致 LOCK TABLE 时也可以请求 InnoDB

表锁。这个选项默认打开,不过可能在 AUTOCOMMIT=1 和 LOCK TABLES 应用中会导致死锁

5.0.3开始,在计算 DECIMAL 值和舍入精确值的时候采用精确数学,DECIMAL 用更有效的格式来存储

从5.0.12开始,自然连接和使用 USING

的连接,包括外部连接的衍生形式,都按照SQL:2003标准来处理了;这个变化导致减少了自然连接和使用 USING

的连接产生的结果字段数,并且还将按照更合理的顺序显示这些字段,逗号比较符的优先顺序和 JOIN, LEFT JOIN 中的一样了

在以前,等待超时的锁会导致 InnoDB 回滚当前全部事务,从5.0.13开始,就只回滚最近的SQL语句了

InnoDB 和 MyISAM 表中空格结尾的 TEXT 字段索引顺序改变了。因此需要运行 "CHECK TABLE" 语句修复数据表,如果出现错误,就运行

"OPTIMIZE TABLE" 或 "REPAIR TABLE" 语句修复,甚至重新转储(用mysqldump)

MySQL 5.0.3到5.0.5之间版本的 MyISAM 和 InnoDB 表中创建的 DECIMAL 字段升级到5.0.6之后会发生崩溃。

以上的相关内容就是对用MySQL内置函数转换ip地址和数字的介绍,望你能有所收获。


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

How to open phpmyadmin How to open phpmyadmin Apr 10, 2025 pm 10:51 PM

You can open phpMyAdmin through the following steps: 1. Log in to the website control panel; 2. Find and click the phpMyAdmin icon; 3. Enter MySQL credentials; 4. Click "Login".

MySQL: An Introduction to the World's Most Popular Database MySQL: An Introduction to the World's Most Popular Database Apr 12, 2025 am 12:18 AM

MySQL is an open source relational database management system, mainly used to store and retrieve data quickly and reliably. Its working principle includes client requests, query resolution, execution of queries and return results. Examples of usage include creating tables, inserting and querying data, and advanced features such as JOIN operations. Common errors involve SQL syntax, data types, and permissions, and optimization suggestions include the use of indexes, optimized queries, and partitioning of tables.

How to use single threaded redis How to use single threaded redis Apr 10, 2025 pm 07:12 PM

Redis uses a single threaded architecture to provide high performance, simplicity, and consistency. It utilizes I/O multiplexing, event loops, non-blocking I/O, and shared memory to improve concurrency, but with limitations of concurrency limitations, single point of failure, and unsuitable for write-intensive workloads.

Why Use MySQL? Benefits and Advantages Why Use MySQL? Benefits and Advantages Apr 12, 2025 am 12:17 AM

MySQL is chosen for its performance, reliability, ease of use, and community support. 1.MySQL provides efficient data storage and retrieval functions, supporting multiple data types and advanced query operations. 2. Adopt client-server architecture and multiple storage engines to support transaction and query optimization. 3. Easy to use, supports a variety of operating systems and programming languages. 4. Have strong community support and provide rich resources and solutions.

MySQL's Place: Databases and Programming MySQL's Place: Databases and Programming Apr 13, 2025 am 12:18 AM

MySQL's position in databases and programming is very important. It is an open source relational database management system that is widely used in various application scenarios. 1) MySQL provides efficient data storage, organization and retrieval functions, supporting Web, mobile and enterprise-level systems. 2) It uses a client-server architecture, supports multiple storage engines and index optimization. 3) Basic usages include creating tables and inserting data, and advanced usages involve multi-table JOINs and complex queries. 4) Frequently asked questions such as SQL syntax errors and performance issues can be debugged through the EXPLAIN command and slow query log. 5) Performance optimization methods include rational use of indexes, optimized query and use of caches. Best practices include using transactions and PreparedStatemen

How to connect to the database of apache How to connect to the database of apache Apr 13, 2025 pm 01:03 PM

Apache connects to a database requires the following steps: Install the database driver. Configure the web.xml file to create a connection pool. Create a JDBC data source and specify the connection settings. Use the JDBC API to access the database from Java code, including getting connections, creating statements, binding parameters, executing queries or updates, and processing results.

phpmyadmin connection mysql phpmyadmin connection mysql Apr 10, 2025 pm 10:57 PM

How to connect to MySQL using phpMyAdmin? The URL to access phpMyAdmin is usually http://localhost/phpmyadmin or http://[your server IP address]/phpmyadmin. Enter your MySQL username and password. Select the database you want to connect to. Click the "Connection" button to establish a connection.

Solution to MySQL encounters 'Access denied for user' problem Solution to MySQL encounters 'Access denied for user' problem Apr 11, 2025 pm 05:36 PM

How to solve the MySQL "Access denied for user" error: 1. Check the user's permission to connect to the database; 2. Reset the password; 3. Allow remote connections; 4. Refresh permissions; 5. Check the database server configuration (bind-address, skip-grant-tables); 6. Check the firewall rules; 7. Restart the MySQL service. Tip: Make changes after backing up the database.

See all articles