在 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的更多相关文章

  1. [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 ...

  2. [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 ...

  3. [LeetCode] Distinct Subsequences 不同的子序列

    Given a string S and a string T, count the number of distinct subsequences of T in S. A subsequence ...

  4. SQL中distinct的用法

    SQL中distinct的用法   1.作用于单列 2.作用于多列 3.COUNT统计 4.distinct必须放在开头 5.其他 在表中,可能会包含重复值.这并不成问题,不过,有时您也许希望仅仅列出 ...

  5. Oracle 查询语句(where,order by ,like,in,distinct)

    select * from production;alter table production add productionprice number(7,2); UPDATE production s ...

  6. mysql中distinct的用法

    本事例实验用表task,结构如下 MySQL> desc task; +-------------+------------+------+-----+-------------------+- ...

  7. Distinct Subsequences

    https://leetcode.com/problems/distinct-subsequences/ Given a string S and a string T, count the numb ...

  8. distinct 与 group by 去重

    例如下表格:表名:fruit id Name Price Num 1 西瓜 10 2 2 西瓜 11 2 3 香蕉 10 3 4 桃子 10 2 当我想获取Name不重复的数据,结果如下 id Nam ...

  9. 【性能为王】从PHP源码剖析array_keys和array_unique

    之前在[译]更快的方式实现PHP数组去重这篇文章里讨论了使用array_flip后再调用array_keys函数替换直接调用array_unique函数实现数组去重性能较好.由于原文没有给出源码分析和 ...

随机推荐

  1. python词云

    词云图 from os import path from PIL import Image import numpy as np import matplotlib.pyplot as plt fro ...

  2. hbase0.94.11版本和hbase1.4.9版本的benchamark区别

    1.起初使用ycsb对hbase进行benchmark,分别在100%写的情况下检测写性能:在100%读的情况下检测读的性能.实验数据如下: 2.新版本的habse写性能竟然不如老版本.!!!.于是我 ...

  3. CodeForces 516C Drazil and Park 线段树

    原文链接http://www.cnblogs.com/zhouzhendong/p/8990745.html 题目传送门 - CodeForces 516C 题意 在一个环上,有$n$棵树. 给出每一 ...

  4. js隐藏元素

    js隐藏元素 $("#serviceType").css('display','none'); js显示元素 $("#serviceType3").css('d ...

  5. JavaSE | 接口| 枚举| 注释| 异常

    包: 1.包的作用:(1)避免类的同名(区分类):类的全名称:包.类名 回忆:java.util.Scannerjava.util.Arraysjava.lang.Stringj(2)可以限定某些类或 ...

  6. 008 pandas介绍

    一:介绍 1.官网 http://pandas.pydata.org/ 2.说明 Python Data Analysis Library 或 pandas 是基于NumPy 的一种工具,该工具是为了 ...

  7. java.io.File中字段的使用

    File.pathSeparator指的是分隔连续多个路径字符串的分隔符,例如:Java   -cp   test.jar;abc.jar   HelloWorld就是指“;” File.separa ...

  8. create-react-app项目添加less配置

    使用create-react-app 创建的项目默认不支持less,以下增加less配置的步骤 暴露配置文件 create-react-app生成的项目文,看不到webpack相关的配置文件,需要先暴 ...

  9. 连接mysql数据库时提示2003 can't connect to MySQL server on ip(10060)的解决办法

    今天部署 JavaWeb 项目到云服务器,突然出现can t connect to MySQL server on ip的问题 经过了一些检查,认为很有可能是防火墙的原因.下面是检查的具体操作: 因为 ...

  10. 色彩空间-- RGB\HSV

    颜色空间 标签(空格分隔): 计算机视觉 颜色通常用三个独立的属性来描述,三个独立变量综合作用,自然就构成一个空间坐标,这就是颜色空间. RGB和CMY颜色模型都是面向硬件的,而HSV(Hue Sat ...