在 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. 【转】git shell 命令大全

    http://www.cnblogs.com/bugs/p/3384339.html 常用命令 git branch 查看本地所有分支 git status 查看当前状态 git commit 提交 ...

  2. echarts Y轴的刻度 跟数据对应---tooltip-formatter

    var xAxisData = ['2018-01', '2018-02', '2018-03', '2018-04', '2018-05', '2018-06', '2018-07', '2018- ...

  3. 51Nod1306 高楼和棋子 动态规划

    原文链接https://www.cnblogs.com/zhouzhendong/p/51Nod1306.html 题目传送门 - 51Nod1306 题意 有个N层的高楼和若干个棋子,所有的棋子都是 ...

  4. HDU1507 Uncle Tom's Inherited Land* 二分图匹配 匈牙利算法 黑白染色

    原文链接http://www.cnblogs.com/zhouzhendong/p/8254062.html 题目传送门 - HDU1507 题意概括 有一个n*m的棋盘,有些点是废的. 现在让你用1 ...

  5. 【基础】链表的储存结构说明(python)

    [实现链表的添加] class aNode(): def __init__(self,data=None,nxt=None): self.data=data self.nxt=nxt class ru ...

  6. POJ2387 Til the Cows Come Home【Kruscal】

    题目链接>>> 题目大意: 谷仓之间有一些路径长度,然后要在这些谷仓之间建立一些互联网,花费的成本与长度成正比,,并且要使这些边连起来看的像一课“树”,然后使成本最大 解题思路: 最 ...

  7. 在windows上安装redis并设置密码

    在windows上安装redis Redis是一个开源,先进的key-value存储,并用于构建高性能,可扩展的Web应用程序的完美解决方案. Redis从它的许多竞争继承来的三个主要特点: Redi ...

  8. 网页布局之grid属性图

  9. How to show color in CSS

    转至:https://blog.csdn.net/CallMeQiuqiuqiu/article/details/54743459 http://www.w3school.com.cn/cssref/ ...

  10. 01 bubbleSort

    #include<cstdio> #include <memory> #include <iostream> using namespace std; void b ...