Home > Backend Development > PHP Tutorial > 这个查询语句应该怎么写才正确

这个查询语句应该怎么写才正确

WBOY
Release: 2016-06-23 13:54:17
Original
954 people have browsed it

$sns=$db->field('DISTINCT(dname)' AS 'd_name','COUNT(dname)' AS 'tjs')->where(array('lid'=>intval($_GET['id']),'islottery'=>1))->GROUP('dname')->order('tjs DESC')->select();


回复讨论(解决方案)

表??,????,查?後?果。
你要提供?些才可以知道?句怎??啊。

DISTINCT 是关键字,而不是函数
不能这样用:DISTINCT(dname)
用了也没效果
但可以放在聚类函数中,比如 count(DISTINCT dname) 统计 dname 有多少种状态

这是我想要的,但是还是一只菜鸟,不知该怎么写!

public function exportTJ(){		//$objReader = PHPExcel_IOFactory::createReader('Excel5');		header("Content-Type: text/html; charset=utf-8");		header("Content-type:application/vnd.ms-execl");		header("Content-Disposition:filename=tongji.xls");		//   以下\t代表横向跨越一格,\n 代表跳到下一行,可以根据自己的要求,增加相应的输出相,要和循环中的对应哈		//字段		$letterArr=explode(',',strtoupper('a,b'));		$arr=array(		array('en'=>'d_name','cn'=>'门店名称'),		array('en'=>'tjs','cn'=>'领取次数'),		);		$chengItem=array('piaomianjia','shuifei','yingshoujine','yingfupiaomianjia','yingfushuifei','yingfujine','dailishouru','fandian','jichangjianshefei','ranyoufei');		$i=0;		$fieldCount=count($arr);		$s=0;		//thead		foreach ($arr as $f){			if ($s<$fieldCount-1){				echo iconv('utf-8','gbk',$f['cn'])."\t";			}else {				echo iconv('utf-8','gbk',$f['cn'])."\n";			}			$s++;		}		//		$db=M('Lottery_record');		$sns=$db->field('DISTINCT(dname) AS d_name,COUNT(dname) AS tjs')->where(array('lid'=>intval($_GET['id']),'islottery'=>1))->GROUP('dname')->order('tjs DESC')->select();		//$sns=$db->where(array('lid'=>intval($_GET['id']),'islottery'=>1))->order('tjs DESC')->select(DISTINCT(dname) AS d_name, COUNT(dname) AS tjs);		//$sns=array('select DISTINCT(dname) AS d_name, COUNT(dname) AS tjs from $db where 'lid'==intval($_GET['id']) and 'islottery'==1 GROUP BY dname ORDER BY tjs DESC');		if ($sns){			if ($sns[0]['token']!=$this->token){				exit('no permission');			}			foreach ($sns as $sn){				$j=0;				foreach ($arr as $field){					$fieldValue=$sn[$field['en']];					switch ($field['en']){						default:							break;											case 'd_name':							$fieldValue=iconv('utf-8','gb2312',$fieldValue);							break;					}					if ($j<$fieldCount-1){						echo $fieldValue."\t";					}else {						echo $fieldValue."\n";					}					$j++;				}				$i++;			}		}		exit();	}
Copy after login

field('dname AS d_name,COUNT(dname) AS tjs')
GROUP('d_name')

麻烦版主再帮我看看,怎么是这个结果呢

public function exportTJ(){
//$objReader = PHPExcel_IOFactory::createReader('Excel5');
header("Content-Type: text/html; charset=utf-8");
header("Content-type:application/vnd.ms-execl");
header("Content-Disposition:filename=tongji.xls");
//   以下\t代表横向跨越一格,\n 代表跳到下一行,可以根据自己的要求,增加相应的输出相,要和循环中的对应哈
//字段
$letterArr=explode(',',strtoupper('a,b'));
$arr=array(
array('en'=>'d_name','cn'=>'门店名称'),
array('en'=>'tjs','cn'=>'领取次数'),
);
$chengItem=array('piaomianjia','shuifei','yingshoujine','yingfupiaomianjia','yingfushuifei','yingfujine','dailishouru','fandian','jichangjianshefei','ranyoufei');

$i=0;
$fieldCount=count($arr);
$s=0;
//thead
foreach ($arr as $f){
if ($s echo iconv('utf-8','gbk',$f['cn'])."\t";
}else {
echo iconv('utf-8','gbk',$f['cn'])."\n";
}
$s++;
}
//
$db=M('Lottery_record');
$sns=$db->where(array('lid'=>intval($_GET['id']),'islottery'=>1,'dname' != null))->field('COUNT(sn) as tjs,dname as d_name')->GROUP('d_name')->order('tjs DESC')->select();
//$sns=$db->where(array('lid'=>intval($_GET['id']),'islottery'=>1))->order('tjs DESC')->select(DISTINCT(dname) AS d_name, COUNT(dname) AS tjs);
//$sns=array('select DISTINCT(dname) AS d_name, COUNT(dname) AS tjs from $db where 'lid'==intval($_GET['id']) and 'islottery'==1 GROUP BY dname ORDER BY tjs DESC');
if ($sns){
if ($sns[0]['token']!=$this->token){
exit('no permission');
}
foreach ($sns as $sn){
$j=0;
foreach ($arr as $field){
$fieldValue=$sn[$field['en']];
switch ($field['en']){
default:
break;
case 'tjs':
    $fieldValue=iconv('utf-8','gb2312',$fieldValue);
    break;
case 'd_name':
$fieldValue=iconv('utf-8','gb2312',$fieldValue);
break;
}
if ($j echo $fieldValue."\t";
}else {
echo $fieldValue."\n";
}
$j++;
}
$i++;
}
}
exit();
}


}

你这种写法应该是thinkphp吧?
据我所知好像tp不支持这样的,不过你可以写原生的sql。

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template