<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>潘锦的空间 &#187; mysql</title>
	<atom:link href="https://www.phppan.com/tag/mysql/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.phppan.com</link>
	<description>SaaS SaaS架构 团队管理 技术管理 技术架构 PHP 内核 扩展 项目管理</description>
	<lastBuildDate>Sat, 25 Apr 2026 00:56:17 +0000</lastBuildDate>
	<language>zh-CN</language>
		<sy:updatePeriod>hourly</sy:updatePeriod>
		<sy:updateFrequency>1</sy:updateFrequency>
	<generator>https://wordpress.org/?v=3.9.40</generator>
	<item>
		<title>锁机制之MySQL表锁</title>
		<link>https://www.phppan.com/2012/11/mysql-table-lock/</link>
		<comments>https://www.phppan.com/2012/11/mysql-table-lock/#comments</comments>
		<pubDate>Mon, 05 Nov 2012 00:23:29 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[程序相关]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[锁机制]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1753</guid>
		<description><![CDATA[如何保证在被并发访问时数据的一致性、完整性和有效性，是数据库关注的核心问题。数据库的锁机制就是为了解决这个问题 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>如何保证在被并发访问时数据的一致性、完整性和有效性，是数据库关注的核心问题。数据库的锁机制就是为了解决这个问题而出现的。锁机制在一定程度上将对共享资源的并发访问有序化，从而保证数据的一致完整性。锁机制的好坏直接影响到数据的并发处理能力和性能。一个好的锁机制的实现是一个数据的核心竞争力之一。</p>
<p>我们知道在MySQL中存在表级锁、页级锁和行级锁，其中MySQL默认实现了表级锁定。其它锁机制在不同的存储引擎中实现，这也是MySQL特点之一：针对特定的应用场景可以使用当前合适的存储引擎。先不论各种存储引擎和锁机制的优劣，这里只是说说他们各自的特点和实现。</p>
<p>MyISAM存储引擎作为曾经的默认存储引擎，其使用的锁机制是MySQL提供的默认表级锁定。虽然它没有实现自己的锁机制，但是在默认表级锁的基础上，增加了并发插入的特性。并发插入与系统参数concurrent_insert相关，concurrent_insert有三个值：</p>
<ul>
<li>concurrent_insert=0 关闭并发写入</li>
<li>concurrent_insert=1 (默认)在没有空数据块的MyISAM表中启用并行插入</li>
<li>concurrent_insert=2  为所有MyISAM表启用并行插入。如果表有空记录或正被另一线程使用，新行将插入到表的最后。如果表未使用，MySQL将进行普通读锁定并将新行插入空记录。</li>
</ul>
<p>此参数与MyISAM存储引擎的数据存储方式相关：常规情况下，MyISAM的新数据都会被附加到数据文件的结尾，当做了一些DELETE操作之后，数据文件就不再是连续的，形象一点来说，就是数据文件里出现了很多hole，此时再插入新数据时，按缺省设置会先看这些hole的大小是否可以容纳下新数据，如果可以，则直接把新数据保存到hole里，反之，则把新数据保存到数据文件的结尾。之所以这样做是为了减少数据文件的大小，降低文件碎片的产生。</p>
<p>如果我们使用concurrent_insert=2（通常也推荐这样做），这样会产生较多的文件碎片，为此，我们需要在设置这个参数值的同时，定期对数据表进行OPTIMIZE  TABLE操作。此操作可以去除删除操作后留下的数据文件碎片，减小文件尺寸，加快未来的读写操作。但是，在OPTIMIZE  TABLE运行过程中，MySQL会锁表。</p>
<p>MySQL的表锁有两种模式：表共享读锁（Table Read Lock）和表独占写锁（Table Write  Lock）。共享锁和独占锁在锁机制中是一种非常普通的实现方式。  MyISAM在执行查询语句前，会自动给涉及的所有表加读锁，在执行更新操作（DDL）前，会自动给相关的表加写锁。  MySQL的读写锁（mysys/thr_lock.c）是通过4个队列来维护的，他们分别是：</p>
<ul>
<li>当前读锁队列（lock-&gt;read）： 存储当前持有读锁所有线程相关信息，按获取锁的时间排序</li>
<li>读锁等待队列（lock-&gt;read_wait）：存储正在等待读锁锁定资源的线程相关信息</li>
<li>当前写锁队列（lock-&gt;write）：存储当前持有写锁所有线程相关信息，按获取锁的时间排序</li>
<li>写锁等待队列（lock-&gt;write_wait）：存储正在等待写锁锁定资源的线程相关信息</li>
</ul>
<p>对于读锁，当请求的资源没有加写锁或在写锁等待队列中没有更高优先级的写锁定在等待。读锁是共享锁，不会阻塞其他进程对同一资源的读请求，但会阻塞对同一资源的写请求。只有当读锁释放后，才会执行其它进程的写操作。</p>
<p>对于写锁，当请求的资源在当前写锁队列、写锁等待队列或当前读锁队列，进入等待写锁队列；写锁会阻塞其他进程对同一资源的读和写操作，只有当写锁释放后，才会执行其它进程的读写操作。</p>
<p>表锁是MySQL数据库中加锁粒度最大的一种锁，除此之外，MySQL还有页级锁和行锁。表锁的执行开销小，加锁速度快，不会出现死锁，但是其加锁的粒度大，发生锁冲突的概率非常高，从而导致并发度低。可以考虑使用主从结构解决并发度低的问题。</p>
<p>参考资料</p>
<p>http://www.zhaokunyao.com/archives/206</p>
<p>http://dev.mysql.com/doc/refman/5.1/zh/database-administration.html</p>
<p>《MySQL性能调优与架构设计》 &#8211; 简朝阳</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2012/11/mysql-table-lock/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>在线修改MySQL大表的表结构</title>
		<link>https://www.phppan.com/2012/05/online-schema-change/</link>
		<comments>https://www.phppan.com/2012/05/online-schema-change/#comments</comments>
		<pubDate>Thu, 10 May 2012 00:57:51 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[程序相关]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[OSC]]></category>
		<category><![CDATA[大数据量]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=1674</guid>
		<description><![CDATA[问题描述 由于某个临时需求，需要给在线MySQL的某个超过千万的表增加一个字段。此表在设计之时完全按照需求实现 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>问题描述</p>
<p>由于某个临时需求，需要给在线MySQL的某个超过千万的表增加一个字段。此表在设计之时完全按照需求实现，并没有多余的保留字段。</p>
<p>我们知道在MySQL中如果要执行ALTER TABLE操作，MySQL会通过制作原来表的一个临时副本来工作。对于表结构的修改在副本上施行，然后将新表替换原始表，此时会产生锁表，用户可以从原始表读取数据，而用户的更新和写入操作都会被lock，待新表准备好后写入新表。<br />
这对于在线的数据量较大的表来说是绝对无法容忍的，并且由于这种在线操作时间会很长，此时如果show processlist，会发现有若干的MySQL进程处于lock状态，当这种进程太多超过单台服务器允许的MySQL进程数，其它进程可能会被拒绝连接。</p>
<p>有哪些方案可以处理这个问题呢？<br />
<br />
<strong>方案1、直接ALTER TABLE</strong><br />
这个方案只能说这仅仅是一种方案，在某些非实时在线或数据量较小时有较好的表现。<br />
<br />
<strong>方案２、模拟数据库修改表结构的操作，在非数据库层实现整个过程。</strong></p>
<ol>
<li>实现业务中对于数据的读写分离</li>
<li>创建一个已经按需求修改好结构的新表</li>
<li>修改业务逻辑，将读操作指向旧表，将写操作指向新表。如果读旧表没有，再读新表，并将旧的数据写入到新表，当然这一步写入操作我们可以不用，我们可以在后台做一个定时任务将旧数据同步到新表。</li>
</ol>
<p>这种方案有一个较大的缺点，需要业务逻辑层配合实现数据的迁移，对于业务逻辑有修改，并且如果有多台机器的话，需要一台一台的修改，较费时间，但是对于MySQL的两种主要存储引擎都适用。</p>
<p> <br />
<strong>方案３、facebook <a href="http://bazaar.launchpad.net/~mysqlatfacebook/mysqlatfacebook/tools/files/head:/osc/">online schema change</a></strong><br />
facebook的OSC在整体流程上与方案2没有较大的区别，只是它在这里引入了触发器，从而不需要修改业务逻辑，在数据库层就实现了新数据的两个表的同步问题。其大概步骤如下：</p>
<ol>
<li>按需求创建新表</li>
<li>针对原始表创建触发器</li>
<li>对于原始表的更新操作都会被触发器更新到新表中</li>
<li>把原始表中的数据复制到新表中</li>
<li>将新表替换旧表</li>
</ol>
<p>fb的osc方案从数据库层解决了方案2的问题，但是它仅支持InnoDB存储引擎。</p>
<p> <br />
<strong>方案４、换一个思路，保留字段。</strong><br />
假设一切可以从头再来，我们也许可以加多一些冗余字段，各个类型都加一些，备用。只是，回不去了！<br />
<br />
<strong>方案5、再换一个思路，增加扩展表。</strong><br />
我们不在原有的表的基础上修改了，以增加扩展表的方式，将新字段的数据写入到扩展表中，修改业务逻辑，这些字段从新表中读取。<br />
<a href="http://liuzhiqiangruc.iteye.com/">志强同学</a>说这是典型的维表结构设计。<br />
暂时解决了问题，如果这些字段后续使用频率高的话，可能会有对后期维护或业务有一定的影响。<br />
<br />
<strong>后记</strong><br />
基于现有的需求，只是需要记录新的字段，所以采用了扩展表的方案。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2012/05/online-schema-change/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>第一次写MySQL存储过程遇到的关于DELIMITER的问题</title>
		<link>https://www.phppan.com/2010/05/mysql-delimit/</link>
		<comments>https://www.phppan.com/2010/05/mysql-delimit/#comments</comments>
		<pubDate>Fri, 07 May 2010 01:18:07 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[DELIMITER]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[存储过程]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=683</guid>
		<description><![CDATA[第一次写MySQL存储过程遇到的关于DELIMITER的问题 在很久很久以前写过Oracle的存储过程，昨天由 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>第一次写MySQL存储过程遇到的关于DELIMITER的问题<br />
在很久很久以前写过Oracle的存储过程，昨天由于某些特殊的原因导致需要在MySQL中写一些存储过程，只能现学现用了<br />
首先写了一个非常简单的存储过程，但是就是如此简单的过程也报错了，其代码如下：</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">&nbsp;
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Hello Word!'</span>;                                                               
<span style="color: #993333; font-weight: bold;">END</span></pre></td></tr></table></div>

<p>在phpmyadmin及客户端都报错，在phpmyadmin中显示：#1064 &#8211; You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near &#8221; at line 3<br />
<br />
显然，程序在第一个分号后面出错了<br />
原因不明，遂google之，找到如下地址：<a href="http://www.cnblogs.com/hsqzzzl/archive/2008/02/21/1076646.html">http://www.cnblogs.com/hsqzzzl/archive/2008/02/21/1076646.html</a><br />
文章作者有说：分隔符是通知MySQL客户端已经输入完成的符号。一直都是用“;”，但是在存储过程中不行，因为存储过程中很多语句都需要用到分号<br />
于是得到如下所示代码</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;">DELIMITER <span style="color: #66cc66;">||</span>
<span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">PROCEDURE</span> test<span style="color: #66cc66;">&#40;</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #993333; font-weight: bold;">BEGIN</span>
    <span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #ff0000;">'Hello Word!'</span>;                              
<span style="color: #993333; font-weight: bold;">END</span> <span style="color: #66cc66;">||</span>
DELIMITER ;</pre></td></tr></table></div>

<p>如果不想使用DELIMITER，在phpmyadmin中执行命令时，在Delimiter文本框中填写||<br />
<br />
另在百度百科中有说明：http://baike.baidu.com/view/3068266.htm<br />
MySQL中delimit命令。<br />
　　这个命令与存储过程没什么关系。<br />
　　其实就是告诉mysql解释器，该段命令是否已经结束了，mysql是否可以执行了。<br />
　　即改变输入结束符。<br />
　　默认情况下，delimiter是分号“;”。<br />
　　在命令行客户端中，如果有一行命令以分号结束，<br />
　　那么回车后，mysql将会执行该命令。<br />
　　但有时候，不希望MySQL这么做。因为可能输入较多的语句，且语句中包含有分号。<br />
　　默认情况下，不可能等到用户把这些语句全部输入完之后，再执行整段语句。<br />
　　因为mysql一遇到分号，它就要自动执行。<br />
　　这种情况下，就可以使用delimiter，把delimiter后面换成其它符号，如//或$$。<br />
　　此时，delimiter作用就是对整个小段语句做一个简单的封装。<br />
　　此命令多用在定义子程序，触发程序等musql自己内嵌小程序中。</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/05/mysql-delimit/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL执行过程</title>
		<link>https://www.phppan.com/2010/02/mysql-process/</link>
		<comments>https://www.phppan.com/2010/02/mysql-process/#comments</comments>
		<pubDate>Fri, 26 Feb 2010 00:30:07 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[程序相关]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=548</guid>
		<description><![CDATA[MySQL执行过程 在用户执行MySQL启动命令后，MySQL的初始化模块从配置文件中读取系统参数和命令行参数 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>MySQL执行过程<br />
在用户执行MySQL启动命令后，MySQL的初始化模块从配置文件中读取系统参数和命令行参数，并按照这些参数初始化整个系统，同时启动并初始化各存储引擎。<br />
在系统初始化结束后，连接管理模块启动监听程序，准备接受客户端的请求。<br />
当一个客户端通过网络连接到MySQL服务器时，连接管理模块会监听到这个请求，通过MySQL自己定义的协议，执行相关的底层任务后，连接管理模块将请求转发给线程管理；<br />
线程管理会提供一个连接线程来处理这个连接，如果线程缓存（Thread Cache）中有空闲的连接线程，那么就会从线程缓存中直接取一个连接线程，否则新创建一个连接线程；</p>
<p>此时，连接线程会调用安全管理模块，进行授权检查，验证用户访问的合法性（用户是否有权访问数据库服务器，用户密码是否正确等）<br />
然后，连接线程开始处理客户端请求所发送过来的命令（或者Query）<br />
对于命令，不需要调用Parse就可以直接执行<br />
对于Query，需要进行Query解析和转发模块的解析，在Query解析器进行分析，如果是一个SELECT类型的Query，则调用查询缓存（Query Cache），检查是否存在相同的查询语句，如果存在则直接将cache中的数据返回，如果不存在或者不是SELECT类型的Query，则将此Query返回解析器。在此时的解析器中，如果是SELECT类型，则它会将控制权交给查询优化器，如果是DML或DDL语句，则会交给表变更管理模块，如果是一些更新统计信息，检测，修复和整理类的Query，则会交给表变更管理模块<br />
这些模块在收到Query解析与分发模块分发过来的请求后，首先会通过访问控制模块检查连接用户是否有访问目标表和目标字段的权限，如果有相关权限，就会调用表管理模块请求相应的表，并获取相应的锁。<br />
在打开表后，根据表的相关meta信息，判断表的存储引擎类型和相关信息。<br />
根据表的存储引擎类型，提交请求给存储引擎接口，调用对应的存储引擎实现模块进行处理。</p>
<p>以上只是正常的流程，在此过程中还有处理失败的返回，验证失败的返回等等。如果MySQL打开了bin-log选项并且操作过程中数据发生的变化，那么相应的模块在处理的过程中会调用日志处理模块，将相应的变更语句以更新事件的形式记录到相关参数的二进制日志文件。</p>
<p>以上内容摘抄自《MySQL性能调优与架构设计》</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/02/mysql-process/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>MySQL中的排序</title>
		<link>https://www.phppan.com/2010/01/mysql-filesort/</link>
		<comments>https://www.phppan.com/2010/01/mysql-filesort/#comments</comments>
		<pubDate>Wed, 20 Jan 2010 00:22:36 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[程序相关]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[数据库]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=505</guid>
		<description><![CDATA[MySQL中的排序 曾经以为filesort是文件排序，后来知道这仅仅是一个排序，文件只是路人甲而已 前些日子 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>MySQL中的排序<br />
<br />
曾经以为filesort是文件排序，后来知道这仅仅是一个排序，文件只是路人甲而已<br />
前些日子从china-pub上买了《Mysql核心内幕》，当浏览到第4章时，以前对MySQL的排序，join buffer不理解的地方，忽然觉得懂了，只是不知道这个懂了是真懂了还是假懂了。<br />
就如当你从一个梦中醒来，难道你就确认你一定不是在做梦了吗？<br />
<br />
说说看了什么吧，<br />
filesort排序算法是将一组记录或元素按照<strong>快速排序算法</strong>放入到内存缓存，然后这几个内存缓存按<strong>合并排序算法</strong>排序。（摘自《Mysql核心内幕》第82页）<br />
filesort有两种模式：<br />
1、<strong>直接模式</strong>，将已经数据完全读取出来，然后进行排序<br />
2、<strong>指针模式</strong>，先根据过滤条件取出排序字段(sort_key)和可以行指针信息(row_id)，根据sort_key排序后，再依据row_id取出取出查询中所请求的其他字段。<br />
第一种算法的优势是减少了数据的二次访问，当然会消耗更多的内存，在算法上来讲是以空间换时间。<br />
MySQL会尽量采用每一种方式，只有在每一种方式不行的情况下才会采用第二种方式（即指针模式）<br />
<br />
然后是MySQL的三种排序方法：<br />
1、使用索引排序<br />
2、在单表上使用filesort排序<br />
3、先使用临时表，再使用filesort排序<br />
（摘自《Mysql核心内幕》第83页）</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2010/01/mysql-filesort/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql各版本之间的差别</title>
		<link>https://www.phppan.com/2009/12/mysql-version-different/</link>
		<comments>https://www.phppan.com/2009/12/mysql-version-different/#comments</comments>
		<pubDate>Thu, 24 Dec 2009 04:01:19 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[程序相关]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=440</guid>
		<description><![CDATA[在PHP面试中经常会遇到关于mysql各版本之间差别的问题， 翻看以前的书籍(《MySQL5 权威指南》)找到 [&#8230;]]]></description>
				<content:encoded><![CDATA[<div style="margin-top: 0px; margin-bottom: 0px;">在PHP面试中经常会遇到关于mysql各版本之间差别的问题，</div>
<div style="margin-top: 0px; margin-bottom: 0px;">翻看以前的书籍(《MySQL5 权威指南》)找到如下答案，另外参考了如下网址的部分内容</div>
<div style="margin-top: 0px; margin-bottom: 0px;"><a style="color: #551a8b;" href="http://dev.mysql.com/doc/refman/5.1/en/roadmap.html">http://dev.mysql.com/doc/refman/5.1/en/roadmap.html</a></div>
<div style="margin-top: 0px; margin-bottom: 0px;">摘抄如下：</div>
<div style="margin-top: 0px; margin-bottom: 0px;"><strong>功能 </strong> <strong>版本(开始支持的版本)</strong></div>
<div style="margin-top: 0px; margin-bottom: 0px;"><strong><br />
</strong></div>
<div style="margin-top: 0px; margin-bottom: 0px;">镜像（动态复制）                                             3.23</div>
<div style="margin-top: 0px; margin-bottom: 0px;">在MyISAM数据表中进行全文搜索              3.23</div>
<div style="margin-top: 0px; margin-bottom: 0px;">BDB数据表开始支持事务                                3.23.34</div>
<div style="margin-top: 0px; margin-bottom: 0px;">InnoDB数据表开始支持事务处理                3.23.34</div>
<div style="margin-top: 0px; margin-bottom: 0px;">InnoDB数据表上的引用集成性检查功能   3.23.34</div>
<div style="margin-top: 0px; margin-bottom: 0px;">==================================</div>
<div style="margin-top: 0px; margin-bottom: 0px;"></div>
<div style="margin-top: 0px; margin-bottom: 0px;">Delete和跨多个数据表的Delete                  4.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">跨多个数据表的UPDATE                               4.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">UNION(合并多个SELECT结果)                   4.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">查询缓冲区（加快重复执行的SQL命令的执行速度） 4.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">嵌入式MySQL库                                               4.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">加密通信（SSL）                                             4.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">InnoDB数据表开始支持热备份                    4.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">适用于客户软件共享函数库的GPL许可证   4.0<br />
================================</div>
<div style="margin-top: 0px; margin-bottom: 0px;"></div>
<div style="margin-top: 0px; margin-bottom: 0px;">子查询                                                                   4.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">支持Unicode(UTF8和UCS2=UTF16)        4.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">支持GIS（GEOMETRY数据类型，R树索引）        4.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">可变语句（带参数的SQL命令）                      4.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">GROUP BY 语句增加ROLLUP子句                 4.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">mysql.user数据表采用了更好的口令字加密算法      4.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">允许单个数据表单独存在一个InnoDB表空间文件里   4.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">======================================</div>
<div style="margin-top: 0px; margin-bottom: 0px;"></div>
<div style="margin-top: 0px; margin-bottom: 0px;">VARCHAR类型的数据列可以容纳超过255个字符      5.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">引入了了BIT数据类型                                          5.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">存储过程                                                                  5.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">触发器                                                       5.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">视图                                                           5.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">游标                                                           5.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">更节约空间的InnoDB表空间格式                          5.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">新的数据库架构管理方案（数据字典，INFORMATION_SCHEMA数据库）5.0</div>
<div style="margin-top: 0px; margin-bottom: 0px;">====================================================</div>
<div style="margin-top: 0px; margin-bottom: 0px;"></div>
<div style="margin-top: 0px; margin-bottom: 0px;">FULL　OUTER　JOIN                     5.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">事件调度                                                5.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">分区                                                        5.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">基于行的备份                                      5.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">插件API                                               5.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">服务器日志表                                     5.1</div>
<div style="margin-top: 0px; margin-bottom: 0px;">外键 6.x （3.23版本中已在innoDB中实现）</div>
<p>以上所示的版本是指开始支持的版本</p>
]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/12/mysql-version-different/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>mysql 中子查询的3个问题</title>
		<link>https://www.phppan.com/2009/11/mysql-sub-query/</link>
		<comments>https://www.phppan.com/2009/11/mysql-sub-query/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 01:46:24 +0000</pubDate>
		<dc:creator><![CDATA[admin]]></dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[mysql]]></category>

		<guid isPermaLink="false">http://www.phppan.com/?p=375</guid>
		<description><![CDATA[【准备】 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 [&#8230;]]]></description>
				<content:encoded><![CDATA[<p>【准备】</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #66cc66;">&#40;</span>
  <span style="color: #ff0000;">`reply_id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span> <span style="color: #993333; font-weight: bold;">AUTO_INCREMENT</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`ticket_id`</span> <span style="color: #993333; font-weight: bold;">INT</span><span style="color: #66cc66;">&#40;</span><span style="color: #cc66cc;">11</span><span style="color: #66cc66;">&#41;</span> <span style="color: #993333; font-weight: bold;">NOT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`reply_created`</span> datetime <span style="color: #993333; font-weight: bold;">DEFAULT</span> <span style="color: #993333; font-weight: bold;">NULL</span><span style="color: #66cc66;">,</span>
  <span style="color: #ff0000;">`reply_content`</span> text<span style="color: #66cc66;">,</span>
  <span style="color: #993333; font-weight: bold;">PRIMARY</span> <span style="color: #993333; font-weight: bold;">KEY</span>  <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">`reply_id`</span><span style="color: #66cc66;">&#41;</span>
<span style="color: #66cc66;">&#41;</span> ENGINE<span style="color: #66cc66;">=</span>InnoDB <span style="color: #993333; font-weight: bold;">DEFAULT</span> CHARSET<span style="color: #66cc66;">=</span>utf8;
&nbsp;
<span style="color: #808080; font-style: italic;">-- ----------------------------</span>
<span style="color: #808080; font-style: italic;">-- Records </span>
<span style="color: #808080; font-style: italic;">-- ----------------------------</span>
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:17:59'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'11111'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'2'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:22:09'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'22222'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:22:19'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'33333'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'4'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:22:28'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'4444'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'5'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'4'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:22:38'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'5555'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'6'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:22:49'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'32'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'7'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:23:18'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'11'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'8'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:23:26'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'43'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'9'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:23:41'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'10'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:23:50'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'1'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'11'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'3'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:24:02'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'11'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'12'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'4'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:24:12'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'f'</span><span style="color: #66cc66;">&#41;</span>;
<span style="color: #993333; font-weight: bold;">INSERT</span> <span style="color: #993333; font-weight: bold;">INTO</span> <span style="color: #ff0000;">`ticket_reply`</span> <span style="color: #993333; font-weight: bold;">VALUES</span> <span style="color: #66cc66;">&#40;</span><span style="color: #ff0000;">'13'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'4'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'2009-11-04 20:24:24'</span><span style="color: #66cc66;">,</span> <span style="color: #ff0000;">'a'</span><span style="color: #66cc66;">&#41;</span>;</pre></td></tr></table></div>

<p>【问题一】</p>
<p>在在In/all/any/sime等子查询里使用limit时会报错：<br />
如下所示SQL<br />
SELECT * FROM `ticket_reply` WHERE reply_id IN (SELECT reply_id * FROM `ticket_reply` LIMIT 3);<br />
报错如下：<br />
#1235 &#8211; This version of MySQL doesn&#8217;t yet support &#8216;LIMIT &#038; IN/ALL/ANY/SOME subquery&#8217; </p>
<p>解决方案，添加一层查询，<br />
SELECT * FROM `ticket_reply` WHERE reply_id IN (SELECT reply_id  FROM (SELECT * FROM `ticket_reply` LIMIT 3) tb )<br />
如果不加别名，则会显示<br />
#1248 &#8211; Every derived table must have its own alias </p>
<p>【问题二】<br />
mysql中分组后取各分组的最新的两组数据</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> s1<span style="color: #66cc66;">.</span> <span style="color: #66cc66;">*</span> 
<span style="color: #993333; font-weight: bold;">FROM</span> ticket_reply <span style="color: #993333; font-weight: bold;">AS</span> s1
<span style="color: #993333; font-weight: bold;">WHERE</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #993333; font-weight: bold;">COUNT</span><span style="color: #66cc66;">&#40;</span> <span style="color: #cc66cc;">1</span> <span style="color: #66cc66;">&#41;</span> 
<span style="color: #993333; font-weight: bold;">FROM</span> ticket_reply <span style="color: #993333; font-weight: bold;">AS</span> s2
<span style="color: #993333; font-weight: bold;">WHERE</span> s2<span style="color: #66cc66;">.</span>reply_created <span style="color: #66cc66;">&gt;=</span> s1<span style="color: #66cc66;">.</span>reply_created
<span style="color: #993333; font-weight: bold;">AND</span> s2<span style="color: #66cc66;">.</span>ticket_id <span style="color: #66cc66;">=</span> s1<span style="color: #66cc66;">.</span>ticket_id
<span style="color: #66cc66;">&#41;</span> <span style="color: #66cc66;">&lt;=</span> <span style="color: #cc66cc;">2</span>
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> ticket_id<span style="color: #66cc66;">,</span> reply_created <span style="color: #993333; font-weight: bold;">DESC</span></pre></td></tr></table></div>

<p>【问题三】<br />
取每个帖子的最新回复</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> 
<span style="color: #993333; font-weight: bold;">FROM</span> <span style="color: #66cc66;">&#40;</span>
<span style="color: #993333; font-weight: bold;">SELECT</span> <span style="color: #66cc66;">*</span> 
<span style="color: #993333; font-weight: bold;">FROM</span> ticket_reply
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> reply_created <span style="color: #993333; font-weight: bold;">DESC</span>
<span style="color: #66cc66;">&#41;</span> newtb
<span style="color: #993333; font-weight: bold;">GROUP</span> <span style="color: #993333; font-weight: bold;">BY</span> ticket_id
<span style="color: #993333; font-weight: bold;">ORDER</span> <span style="color: #993333; font-weight: bold;">BY</span> reply_created <span style="color: #993333; font-weight: bold;">DESC</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>https://www.phppan.com/2009/11/mysql-sub-query/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
