php学习记录三:查询

以下记录几种常见情景通过php查询数据库

1、查询表中所有数据

$query="select * from article_category";

模糊查询

select * from t_xj_cp_info where title like '茄子%' limit 0 , 15;
//查询筛选条件在一个数组中
$id_arr='1,2,3,4,5';
$query="select * from bz_list where id in ({$id_arr})";

判断结果非空

$result=mysqli_query($link, $query);
if($result){
	while ($row=mysqli_fetch_array($result)){
		//有结果,则返回
	}
}else{
    //无结果
}

2、限制查询条数(下标0-15的数据)

$query="select * from article_tags limit 0 , 15";

3、如果使用分页的话(传入分页参数)
COUNT:统计记录的数量

$page=var_is_null2('get','page');//获取参数,当前第几页
$num=var_is_null2('get','num');//获取参数,每页条数
$current_num=($page-1)*$num;//查询时的初始下标
//sql语句
$query="select * from article limit {$current_num} , {$num}";
//获取总条数
$rowcount = search_all_row($link,"select count(*) from article");

 MAX\MIN:获取最大值和最小值,可以是任何数据类型,但只能获取一个字段

$sql="select max(all_count) from table" //获取table表中all_count字段最大的数
$sql="select min(all_count) from table" //获取table表中all_count字段最小的数
$sql="select max(CONVERT(all_count,UNSIGNED)) from table" //CONVERT将varchar类型转为数字

 AVG\SUM:获取平均值、总和

$sql="SELECT SUM(count) AS nums FROM table" //查询table中所有count字段的值并求和,以nums输出
$sql="SELECT SUM(count+count2) AS nums FROM bable" //查询table中所有count+count2字段的总和并求和
//查询table中每行id,count+count2的值
$sql="SELECT id,count+count2 AS nums FROM table GROUP BY id"

4、单个条件查询(查询article_tags_relation表中tags_id字段=变量$tags的数据)

查询条件> < >= <= = !=,与 或(AND,OR),在 不在(IN,NOT IN),在[a,b] (between val1 and val2),空 非空(NULL,NOT NULL),排重DISTINCT

$tags=1;
$query="select * from article_tags_relation where tags_id = '{$tags}'"

5、多个条件查询(查询表中  year=$year 且 mouth=$mouth  的数据)

$query="select * from article where year = '{$year}' and mouth = '{$mouth}' limit {$current_num} , {$num}";

6、数据排序(通过ORDER BY 字段  排序,设置升序降序ASC,DESC)

注:多项排序:格式:select 字段 from 表名 where 条件 ORDER BY 字段 ASC|DESC,字段ASC|DESC

$query="select * from article where year = '{$year}' and mouth = '{$mouth}' order BY time desc limit {$current_num} , {$num}";

7、分组查询(GROUP BY)

       a、from 表名,先确定数据的来源
  b、where 确定表中的哪些数据有效
  c、group by 字段名,确定分组的依据,即b条件筛选后的数据取article_id
  d、分组后同时满足article表中article_state = 2的条件

$query="select * from article_tags_relation where tags_id = '{$tags}' and article_id in (select article_id from article where article_state = 2) GROUP BY article_id limit {$current_num} , {$num}";

$query="select * from user where user.id not in (select user_id from role_user_relation where role_id = '{$role_id}')";

8、多表查询

方式一(通用型):SELECT … FROM … WHERE

SELECT * FROM article_tags,article_tags_relation where article_tags.tags_id = article_tags_relation.tags_id and article_tags.tags_id = 2

 

select u.user_id,u.user_name,b.adress from no_acount_list u,acount_list b where u.user_id = b.user_id

方式二:SELECT … FROM … NATURAL JOIN …

有局限性:会自动连接两个表中相同的列(可能有多个:department_id和manager_id)

SELECT last_name,department_id,department_name

FROM employees

NATURAL JOIN departments

方式三:SELECT … JOIN … USING …

有局限性:好于方式二,但若多表的连接列列名不同,此法不合适

SELECT last_name,department_id,department_name

FROM employees

JOIN departments

USING(department_id)

方式四:SELECT … FROM … JOIN … ON …

常用方式,较方式一,更易实现外联接(左、右、满)

select * from user right join notice on user.id = notice.id;

处理查询得到的结果

一般使用mysqli_fetch_array()处理——从结果集中取得一行作为关联数组,或数字数组,或二者兼有

//$result维查询获得的结果集
$arr = array();//创建结果集数组
while ($row=mysqli_fetch_array($result)){//逐条获取
        //创建对象数组
        $arr1=array(
            'article_id'=>$row["article_id"],
            'article_title'=>$row["article_title"],
            'user_id'=>$row["user_id"],
            'content'=>$row["article_content"],
            'article_state'=>$row["article_state"],
            'create_time'=>$row["time"]
        );
        //加入结果集数组
        array_push($arr,$arr1);
    }

最后使用json_encode()输出

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值