在 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全栈开发day78、79 --bss项目

    一.回顾 1. BBS项目 CMS 1. 登录 1. form组件 2. auth模块 3. 验证码 2. 注册 1. form组件 1. 生成html代码 直接for循环form_obj,就能够遍历 ...

  2. Python_时间复杂度概念

    时间频度:一个算法中的语句执行次数称为语句频度或时间频度,记为T(n)(T代表次数,n代表问题规模) 时间复杂度:呈现时间频度的变化规律,记为T(n)=O(f(n)) 指数时间:一个问题求解所需的执行 ...

  3. day8.登陆注册简单实现

    username = input('请输入注册的用户名:') password = input('请输入注册名的密码:') with open('list_of_info',mode='w',enco ...

  4. {}动态规划}记忆化dp

    先搞个模板 #include<stdio.h> #include<string.h> using namespace std; typedef long long ll; ]; ...

  5. json 对象和json字符串

    转载至  http://www.cnblogs.com/cstao110/p/3762056.html JSON字符串与JSON对象的区别 Q:什么是"JSON字符串",什么是&q ...

  6. UVa 679 - Dropping Balls【二叉树】【思维题】

    题目链接 题目大意: 小球从一棵所有叶子深度相同的二叉树的顶点开始向下落,树开始所有节点都为0.若小球落到节点为0的则往左落,否则向右落.并且小球会改变它经过的节点,0变1,1变0.给定树的深度D和球 ...

  7. cmd使用notepad++为打开方式打开文件

    想放一个txt进入vstart中,但是又不想用系统自带的记事本打开,想在vstart中双击时使用notepad++打开. cmd命令如下: "D:\notepad++\notepad++.e ...

  8. Python常用模块--configparser

    作用: 官方:实现基本配置语言的类,该语言提供类似于Microsoft Windows INI文件中的结构.您可以使用它来编写可由最终用户轻松定制的Python程序. 通俗的说:该模块用于系统配置文件 ...

  9. Django初识 学习笔记一

    Django初识 学习笔记一 mvcviewsmodelstemplate. 一 MVC框架 MVC全名是Model View Controller,是模型(model)-视图(view)-控制器(c ...

  10. JS 实现打印

    <input id="btnPrint" type="button" value="打印预览" onclick=preview(1) ...