mysql 判断两个数组是否有交集

mysql 判断两个数组是否有交集,第1张

在PHP中求数组的交集,我们可以与PHP给我们提供的现成函数:array_intersect(),其用法格式为:

array array_intersect(array array1,array array2[,arrayN…])

根据上述的语法格式,我们来写一个例子:

1<?php

2$fruit1 = array("Apple","Banana","Orange")

3$fruit2 = array("Pear","Apple","Grape")

4$fruit3 = array("Watermelon","Orange","Apple")

5$intersection = array_intersect($fruit1, $fruit2, $fruit3)

6print_r($intersection)

7// 输出结果:

8// Array ( [0] =>Apple )

9?>

本例子将返回在$fruit1数组中出现且在$fruit2和$fruit3中也出现的所有水果的名子。

使用array_intersect()函数时要注意:只有在两个元素相等且具有相同的数据类型时,array_intersect()函数才会认

为它们是相同的,否则不能进行交集计算。array_intersect()函数返回一个保留了键的数组,只由第一个数组中出现的且在其它数组中都出现的

值组成。

若要求关联数组的交集,请使用array_intersect_assoc()函数,给你个简单的例子:

1<?php

2$fruit1 = array("red"=>"Apple","yellow"=>"Banana","orange"=>"Orange")

3$fruit2 = array("yellow"=>"Pear","red"=>"Apple","purple"=>"Grape")

4$fruit3 = array("green"=>"Watermelon","orange"=>"Orange","red"=>"Apple")

5$intersection = array_intersect_assoc($fruit1, $fruit2, $fruit3)

6print_r($intersection)

7// 输出:

8// Array ( [red] =>Apple )

9?>

array_intersect_assoc()函数语法格式如下:

array array_intersect_assoc(array array1,array array2[,arrayN…])

array_intersect_assoc()与array_intersect()基本相同,只不过他在比较中还考虑了数组的键。因此,只有在第一个数组中出现,且在所有其他输入数组中也出现的键/值对才返回到结果数组中。

有两种方法,一种方法使用mysql的check table和repair table 的sql语句,另一种方法是使用MySQL提供的多个myisamchk, isamchk数据检测恢复工具。前者使用起来比较简便。推荐使用。

1. check table 和 repair table

登陆mysql 终端:

mysql -uxxxxx -p dbname

check table tabTest

如果出现的结果说Status是OK,则不用修复,如果有Error,可以用:

repair table tabTest

进行修复,修复之后可以在用check table命令来进行检查。在新版本的phpMyAdmin里面也可以使用check/repair的功能。

2. myisamchk, isamchk

其中myisamchk适用于MYISAM类型的数据表,而isamchk适用于ISAM类型的数据表。这两条命令的主要参数相同,一般新的系统都使用MYISAM作为缺省的数据表类型,这里以myisamchk为例子进行说明。当发现某个数据表出现问题时可以使用:

myisamchk tablename.MYI

进行检测,如果需要修复的话,可以使用:

myisamchk -of tablename.MYI

关于myisamchk的详细参数说明,可以参见它的使用帮助。需要注意的时在进行修改时必须确保MySQL服务器没有访问这个数据表,保险的情况下是最好在进行检测时把MySQL服务器Shutdown掉。


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/8545415.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-17
下一篇 2023-04-17

发表评论

登录后才能评论

评论列表(0条)

保存