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

yipeiwu_com6年前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实现XML与数据格式进行转换类实例

本文实例讲述了PHP实现XML与数据格式进行转换类。分享给大家供大家参考。具体如下: <?php /** * xml2array() will convert the...

php 启动报错如何解决

复制代码 代码如下: [root@abc lnmp]# service php-fpm start Starting php-fpm eAccelerator: Could not al...

php number_format() 函数通过千位分组来格式化数字的实现代码

定义和用法number_format() 函数通过千位分组来格式化数字。 语法number_format(number,decimals,decimalpoint,separator)...

PHP数据库表操作的封装类及用法实例详解

本文实例讲述了PHP数据库表操作的封装类及用法。分享给大家供大家参考,具体如下: 数据库表结构: CREATE TABLE `test_user` ( `id` int(11) NO...

关于file_get_contents返回为空或函数不可用的解决方案

如果你使用file_get_contents获取远程文件内容返回为空或提示该函数不可用,也许本文能帮到你! 使用file_get_contents和fopen必须空间开启allow_ur...