php操作mysql 取 并集、交集、差集(对于同样结构的表)
2020-03-01 本文已影响0人
Julianne_zhang
并集
mysqli_query($conn," truncate table usef ");
mysqli_query($conn,"INSERT INTO usef SELECT * FROM temptemp UNION SELECT * FROM tempff " );
交集
mysqli_query($conn," truncate table usef ");
mysqli_query($conn,"INSERT INTO usef select tempff.* from tempff inner join temptemp on tempff.userid = temptemp.userid and tempff.riqi = temptemp.riqi ");
差集
mysqli_query($conn," truncate table usef ");
mysqli_query($conn,"INSERT INTO usef select distinct temptemp.* from tempff inner join temptemp on (tempff.userid = temptemp.userid and tempff.riqi = temptemp.riqi)");
注释:$conn 连接文件名
两个表名:temptemp 和 tempff
生成的表名:usef
以上三个表的结构都是一样的。
差集上比较麻烦,试了很多都不是我要的,最后这样的写法成功了,注意我的主键是两个哦,参考的亲们根据自己情况定,还有就是尽量不要用not in,效率很低,我用了内联的写法。