thinkphp中多表查询中防止数据重复的sql语句(必看)

yipeiwu_com7年前PHP代码库

下面先来看看例子:

table

id name
1 a
2 b
3 c
4 c
5 b

库结构大概这样,这只是一个简单的例子,实际情况会复杂得多。

select *, count(distinct name) from table group by name

结果:

id name count(distinct name)
1 a 1
2 b 1
3 c 1

最后一 项是多余的,不用管就行了

tp2.0手册   搜索连贯操作 可看到相关的资料

SELECT cat_id, COUNT(*) AS goods_num FROM talble GROUP BY cat_id

$M = M('table');
$rs = $M->field(array('cat_id','count(*)'=>'goods_num'))->group('cat_id')->select();

echo $M->getLastSQL();
print_r($rs);

以上就是小编为大家带来的thinkphp中多表查询中防止数据重复的sql语句(必看)的全部内容了,希望对大家有所帮助,多多支持【宜配屋www.yipeiwu.com】~

相关文章

php生成图形验证码几种方法小结

我们先来检查一下自己的php是不是打开了gd库。复制代码 代码如下:<?phpif(extension_loaded('gd')) {  echo '你可以使用gd<...

PHP 反向排序和随机排序代码

array_reverse()函数与shuffle()函数介绍 array_reverse() array array_reverse(array)array_reverse()函数传入...

PHP中date()日期函数有关参数整理

在页面的最前页加上 date_default_timezone_set(PRC); /*把时间调到北京时间,php5默认为格林威治标准时间*/ date () a: "am"或是"pm"...

php强制下载类型的实现代码

复制代码 代码如下: function downloadFile($file){ /*Coded by Alessio Delmonti*/       &...

PHP学习笔记(三):数据类型转换与常量介绍

一、PHP数据类型相互转换 1、强制转换 复制代码 代码如下: // bool,int,float,string,array,object,null bool settype ( mix...