用distinct or array_unique
在 Mysql 获取数据时,如果想获取某一列去重数据,如果获取呢
举个例子:
advert_pro_ad 表
CREATE TABLE `advert_pro_ad` (
`advert_id` int() NOT NULL DEFAULT '' COMMENT '广告id',
`pro_id` int() NOT NULL DEFAULT '' COMMENT '项目id',
UNIQUE KEY `uniq_pro_aid` (`advert_id`,`pro_id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COMMENT '广告和广告项目关联表';
项目和广告是一对多的关系。如何获取去重之后的项目Id呢?
有三种办法
1,
select distinct(pro_id) from advert_pro_ad order by pro_id desc
2,
select pro_id from advert_pro_ad order by pro_id desc
把数据取出来之后,再用 array_unique 去重
3,
select pro_id from advert_pro_ad group by pro_id order by pro_id desc
建议使用第一种,使用第二种会有以下弊端
1、进程间IO通讯暴增。从mysql会向php传大量的数据。IO通讯是最影响速度的。
2、内存限制。PHP是内存操作。通常默认执行内存为128M,能处理的数据量只会大大小于128M.
除非改默认设置到较大值,加大内存开销。
3、效率较差。不仅从mysql到php有复制,而且array_unique效率也mysql DISTINCT差。
转自:https://stackoverflow.com/questions/19473869/select-distinct-or-array-unique
用distinct or array_unique的更多相关文章
- [LeetCode] Longest Substring with At Most K Distinct Characters 最多有K个不同字符的最长子串
Given a string, find the length of the longest substring T that contains at most k distinct characte ...
- [LeetCode] Longest Substring with At Most Two Distinct Characters 最多有两个不同字符的最长子串
Given a string S, find the length of the longest substring T that contains at most two distinct char ...
- [LeetCode] Distinct Subsequences 不同的子序列
Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...
- SQL中distinct的用法
SQL中distinct的用法 1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...
- Oracle 查询语句(where,order by ,like,in,distinct)
select * from production;alter table production add productionprice number(7,2); UPDATE production s ...
- mysql中distinct的用法
本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...
- Distinct Subsequences
https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...
- distinct 与 group by 去重
例如下表格:表名:fruit id Name Price Num 1 西瓜 10 2 2 西瓜 11 2 3 香蕉 10 3 4 桃子 10 2 当我想获取Name不重复的数据,结果如下 id Nam ...
- 【性能为王】从PHP源码剖析array_keys和array_unique
之前在[译]更快的方式实现PHP数组去重这篇文章里讨论了使用array_flip后再调用array_keys函数替换直接调用array_unique函数实现数组去重性能较好.由于原文没有给出源码分析和 ...
随机推荐
- 【Arduino】开源开发板说明
来自世界各地的新型微控制器层出不穷,这类开发板多数都是通过Arduino改进的版本,例如由Arduino所改良的Yún一样,主要是针对网状网路进行改进或升级了其它无线功能. 但一些开发板也有着其独到的 ...
- Python_copy_深浅拷贝
对于数字和字符串来说,无论是‘’赋值‘’还是‘’深拷贝‘’还是‘’浅拷贝‘’都是指向的同一个地址 深浅拷贝是copy类下的方法,创建方式为 import copy copy.copy() #浅拷贝 c ...
- Java中字符串比较的问题
package com.hxl; import java.util.Scanner; public class Test { public static void main(String[] args ...
- IDEA控制台问题:java lang OutOfMemoryError:PermGen space
PermGen space的全称是Permanent Generation space,是指内存的永久保存区域. OutOfMemoryError: PermGen space从表面上看就是内存溢出, ...
- Python编程基础[函数和面向对象](三)
Python 函数 函数是组织好的,可重复使用的,用来实现单一,或相关联功能的代码段. 函数能提高应用的模块性,和代码的重复利用率.你已经知道Python提供了许多内建函数,比如print().但你也 ...
- Linux下C语言的文件操作
代码: #include <stdio.h> #include <string.h> #include <fcntl.h> /*************基本的函数A ...
- 12306登录爬虫 session版本
import requests import re import base64 # 定义session headers = { 'User-Agent':'Mozilla/5.0 (Windows N ...
- Xamarin Essentials教程屏幕常亮ScreenLock
Xamarin Essentials教程屏幕常亮ScreenLock 在一段时间内,如果用户没有对设备进行操作,设备就会自动关闭屏幕.用户必须手动操作,才能点亮屏幕.但是很多应用需要在用户没有操作 ...
- react组件通信那些事儿
父组件是调用组件的组件.现在看来,感觉父组件就是一个壳子,定义好壳子里面会有什么,而子组件是一个具体的实现,说明,会用到什么东西,如果有这些东西,会进行什么操作.总之,父组件是材料,有水和泥,子组件告 ...
- 如何解决Failed to retrieve MSVC Environment from XXXXXXXX
升级了新版的Qt5.9.3后,本人的电脑也出了这个问题. 最后通过删除了path中的一些错误.多余的环境变量解决了.(删除了一些mysql的环境变量)