SQL语句对返回的结果集里的字段再进行条件查询,在一个结果集显示

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SQL语句对返回的结果集里的字段再进行条件查询,在一个结果集显示相关的知识,希望对你有一定的参考价值。

这个sql可以通过将初始查询结果集用括号括起来当作一个子查询(也可以称之为虚拟表),然后再对其实施进一步筛选从而获得另一个结果集的办法予以实现。
例如列出各科平均分在80到90分的同学姓名和各科平均分
select stu.name,t.平均分 from stu,
(select sid,avg(score) as 平均分 from scores group by sid)t
where stu.sid=t.sid and t.平均分 between 80 and 90;
事实上Sql可以通过这种方法实现多重子查询筛选、嵌套和连接非常方便。
参考技术A 这个sql可以通过将初始查询结果集用括号括起来当作一个子查询(也可以称之为虚拟表),然后再对其实施进一步筛选从而获得另一个结果集的办法予以实现。
例如列出各科平均分在80到90分的同学姓名和各科平均分
select stu.name,t.平均分 from stu,
(select sid,avg(score) as 平均分 from scores group by sid)t
where stu.sid=t.sid and t.平均分 between 80 and 90;
事实上Sql可以通过这种方法实现多重子查询筛选、嵌套和连接非常方便。

通过不同的条件组装 一个sql语句

通过不同的条件组装 一个sql语句
题目要求:目前需要通过不同会员信息的条件,对会员进行高级查询,这个时候就需要对sql语句的组装工作。需要查询的条件有,会员编号,会员昵称,会员帐号,会员注册时间,会员性别,条件通过GET方式传递给程序,通过任意选择这些条件进行查询,得到相应的结果。
参考:
会员表为user_bas 及其字段结构;
查询条件和数据库字段对应关系:
会员编号:id ;结构:smallint(6),查询为精确查询(比如:查询编号为1 2的会员)
会员昵称:nickname;结构:varchar(20) (比如:查询昵称里有”魅力”的昵称)
会员帐号:uname;结构:varchar(16) 查询为模糊查询
会员注册时间:regtime;结构:int(10) 查询为时间段查询
会员性别:sex;结构:tingint(1) 查询为 查询为精确查询
目标:
写一段程序,实现这样的查询功能,”查询条件”需要的时候会增加和减少,考生需要编写一套灵活的程序
注意:
1。灵活性,
用户可以选择1个条件,也可以一个都不选择,也可以选择多个。”
2。考虑到系统的性能
有的面试者用:
“select * from user_bas where id=’$id’ and nickname=’$nickname’ and sex=’$sex’…”
是不可以的,如果没有$id的条件数据库也还要去便历,在超过10万的数据库就非常慢了。
3。不用考虑接受数据问题,和传递给数据库如何解释的问题。
如果你怎么接受不知道如何书写,你可以更简单的思考,现在有几个变量。$id,$nickname,$uname,$sex,$regtime 可以用这些变量,但有的为空;对于数据库如何去查询,还有就是,数据库如何解释不用关心,如query相关函数语句你不用写。
4.不允许使用”where 1″或”where 1=1″类型的语法

 

代码
_________________________________________________________________________

<?php
if($_SERVER[‘REQUEST_METHOD‘]==’GET’){
$id=$_GET[‘id‘];
$nickname=$_GET[‘nickname‘];
$uname=$_GET[‘uname‘];
$regtime=$_GET[‘regtime‘];
$sex=$_GET[‘sex‘];
}
$ConditionsNumber=5;
$ConditionsArray=array(“$id “,”$nickname”,”$uname”,”$regtime”,”$sex”);
$SearchSQLArray=array(” where id =’$id’”,” where nickname = ‘$nickname’”,” where uname=’$uname’”,” where regtime=’$regtime’”,” where sex=’$sex’”);
for($i=0;$i<$ConditionsNumber;$i++)
{
if($ConditionsArray[$i]==”")
$SearchSQLArray[$i]=”";
$haveWhere=false;
for($j=0;$j<$i;$j++)
{
$wherePosition=strpos($SearchSQLArray[$j],”where”);
if(($wherePosition==”1″)&&($haveWhere==false))
{
$SearchSQLArray[$i]=ereg_replace(“where”,”and”,$SearchSQLArray[$i]);
$haveWhere=true;
} } }}
for($i=0;$i<$ConditionsNumber;$i++)
$sql=$sql.$SearchSQLArray[$i];
$query=”SELECT * FROM user_bas “.$sql.” order by id;”;
?>

3.目前有会员登陆的log记录数组,登陆日志里面有这个会员的编号(id)、会员帐号(uname)和登陆时间(logtime)。
通过这个数组需要得到如下统计:
(1)得到某一天中不同会员的登陆情况,一天内会员登陆了4次,就记4次。统计结果如:
在2006-07-20内的会员登陆情况
会员编号 会员帐号 会员登陆次数
1 User1 4次
3 User3 2次
2 User2 1次
(2)得到某几天内不同会员的登陆情况,会员在某一天登陆了很多次只记一次(也可以说会员在几天内有几天登陆了)。统计结果如:
在2006-07-20 到2006-07-22 的3天时间内的会员登陆情况:
会员编号 会员帐号 会员登陆次数
3 User3 3次
1 User1 2次
2 User2 2次
附件:
会员登陆日志数组:
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-22 19:18:02′);
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-22 18:15:03′);
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-22 13:50:12′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-22 13:12:09′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-22 11:10:08′);
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-22 10:52:54′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-22 08:16:04′);

$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 19:18:02′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 18:15:03′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 13:50:12′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-21 13:12:09′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-21 11:10:08′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 10:52:54′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 08:16:04′);

$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-20 19:18:02′);
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-20 18:15:03′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-20 13:50:12′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-20 13:12:09′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-20 11:10:08′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-20 10:52:54′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-20 08:16:04′);

代码:
______________________________________________________________________________
<?php
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-22 19:18:02′);
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-22 18:15:03′);
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-22 13:50:12′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-22 13:12:09′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-22 11:10:08′);
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-22 10:52:54′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-22 08:16:04′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 19:18:02′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 18:15:03′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 13:50:12′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-21 13:12:09′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-21 11:10:08′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 10:52:54′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-21 08:16:04′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-20 19:18:02′);
$userlog[] = array (“id” => 2, “uname” => ‘user2′, “logtime” => ’2006-07-20 18:15:03′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-20 13:50:12′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-20 13:12:09′);
$userlog[] = array (“id” => 3, “uname” => ‘user3′, “logtime” => ’2006-07-20 11:10:08′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-20 10:52:54′);
$userlog[] = array (“id” => 1, “uname” => ‘user1′, “logtime” => ’2006-07-20 08:16:04′);
//第一题
$user=”;
$date=’2006-07-20′;
foreach($userlog as $val)
{
if(!isset($user[$val[‘id‘]]))
{
$user[$val[‘id‘]][‘id‘]=$val[‘id‘];
$user[$val[‘id‘]][‘uname‘]=$val[‘uname‘];
$user[$val[‘id‘]][‘logcounts‘]=0;
}
if(strstr($val[‘logtime‘],$date))
{
$user[$val[‘id‘]][‘logcounts‘]+=1;
}
}
echo ‘’;
print_r($user);
echo ‘’;
//第二题
$users=”;
$start=strtotime(’2006-07-20′);
$end =strtotime(’2006-07-22′)+3600*24;
$time=”;
foreach($userlog as $val)
{
if(!isset($users[$val[‘id‘]]))
{
$users[$val[‘id‘]][‘id‘]=$val[‘id‘];
$users[$val[‘id‘]][‘uname‘]=$val[‘uname‘];
$users[$val[‘id‘]][‘days‘]=array();
$users[$val[‘id‘]][‘logs‘]=0;
}
$time=strtotime($val[‘logtime‘]);
if($time>=$start & $time<$end)
{
if(!in_array(substr($val[‘logtime‘],0,10),$users[$val[‘id‘]][‘days‘]))
{
$users[$val[‘id‘]][‘days‘][]=substr($val[‘logtime‘],0,10);
$users[$val[‘id‘]][‘logs‘]+=1;
}
}
}
echo ‘’;
print_r($users);
echo ‘’;
?>

来源http://www.jobui.com/mianshiti/it/php/4225/

 

以上是关于SQL语句对返回的结果集里的字段再进行条件查询,在一个结果集显示的主要内容,如果未能解决你的问题,请参考以下文章

SQL查询语句查询多个数据

MySQL高级SQL语句

通过不同的条件组装 一个sql语句

SQL查询结果替换???????

将多条查询结果作为like查询条件

sql语句中如何一个字段对查询出的多个值添加判断条件