php中查询mysql select 第一个字段 第二个字段 from 表名。输出语句应
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了php中查询mysql select 第一个字段 第二个字段 from 表名。输出语句应相关的知识,希望对你有一定的参考价值。
php中查询mysql select 第一个字段 第二个字段 from 表名。输出语句应该怎么写,才能使第一行输出的是一个字段的第一个值。第二行是第二字段的第一个到第三个值。然后第三个行是第一个字段的第二个值,第四行是第二个字段的四到六的值,应该怎么写,成功实现输出这样的效果
获得结果化为数组
while($row=mysql_fetch_array($query))
$array[]=$row;
2.循环数组输出
for($i=0;$i<count($array);$i++)
echo $array[$i]['字段1'];
echo "<br/>";
if($i%3!=0)
echo $array[3*$i+1]['字段2'];
echo $array[3*$i+2]['字段2'];
echo "<br/>";
else
echo $array[3*$i]['字段2'];
}
参考技术A 一样追问怎么写的
PHP之路——MySql查询语句
1,select查询的基本结构
select 字段 from 表 where 过滤条件 group by 分组条件 order by 排序条件 having 过滤的第二条件 limit 限定结果条件;
2,最简单的查询语句
mysql> select * from user; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+
3,查询一列或多列
mysql> select username,createtime from user; +----------+---------------------+ | username | createtime | +----------+---------------------+ | kenan | 2012-10-31 15:32:26 | | kenan | 2012-10-31 15:32:26 | +----------+---------------------+ 2 rows in set (0.00 sec)
4,多表查询
mysql> select * from user; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 2 rows in set (0.00 sec) mysql> select * from teacher; +----+------------+---------------------+ | id | username | createtime | +----+------------+---------------------+ | 1 | Teacher Li | 2012-11-01 21:00:58 | | 2 | Teacher Le | 2012-11-01 21:02:07 | +----+------------+---------------------+ 2 rows in set (0.00 sec) mysql> select * from user,teacher; +----+----------+----------+---------------------+----+------------+---------------------+ | id | username | password | createtime | id | username | createtime | +----+----------+----------+---------------------+----+------------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | 1 | Teacher Li | 2012-11-01 21:00:58 | | 3 | kenan | lele | 2012-10-31 15:32:26 | 1 | Teacher Li | 2012-11-01 21:00:58 | | 1 | kenan | kenan | 2012-10-31 15:32:26 | 2 | Teacher Le | 2012-11-01 21:02:07 | | 3 | kenan | lele | 2012-10-31 15:32:26 | 2 | Teacher Le | 2012-11-01 21:02:07 | +----+----------+----------+---------------------+----+------------+---------------------+ 4 rows in set (0.00 sec) 多表查询中,如果没有限制条件的话,两个表的记录会分别进行匹配,产生的结果就是两个表的笛卡尔积,叫做全连接 下面的这个带有条件的叫做等同连接,相当于在上面的查询结果中挑选出来满足where后面条件的记录,因为没有所有是空的结果。
mysql> select * from user,teacher where user.username = teacher.username; Empty set (0.00 sec)
5,带有简单的where字句条件查询
mysql> select * from user where id>2; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec)
6,带有in的查询,in用来表示范围
mysql> select * from user where username in (‘kenan‘,‘Micheal‘); +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 2 rows in set (0.00 sec)
这里查询出来username是kenan或者是Micheal的记录
7,between and 用来表示一个字段的范围的查询
mysql> select * from user where id between 2 and 3; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec) mysql> select * from user where id between 3 and 4; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec)
这里分别查询出来id在2和3之间的记录,3到4之间的记录,从结果可以看出 between and是包含边界的,就是>=2而且<=3
8, is null 查询空值
mysql> select * from user ; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | | 4 | lele | lele | NULL | +----+----------+----------+---------------------+ 3 rows in set (0.00 sec) mysql> select * from user where createtime is null; +----+----------+----------+------------+ | id | username | password | createtime | +----+----------+----------+------------+ | 4 | lele | lele | NULL | +----+----------+----------+------------+ 1 row in set (0.00 sec)
9,用and连接多条件查询 并关系
mysql> select * from user where username = ‘kenan‘ and password = ‘kenan‘; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 1 row in set (0.00 sec)
10,用or 的多条件查询 这个是或关系,满足一个条件即可
mysql> select * from user where username = ‘kenan‘ or password = ‘kenan‘; +----+----------+----------+---------------------+ | id | username | password | createtime | +----+----------+----------+---------------------+ | 1 | kenan | kenan | 2012-10-31 15:32:26 | | 3 | kenan | lele | 2012-10-31 15:32:26 | +----+----------+----------+---------------------+ 2 rows in set (0.00 sec)
11,聚合函数 count()用来统计总数
mysql> select count(*) from user; +----------+ | count(*) | +----------+ | 3 | +----------+ 1 row in set (0.00 sec)
12,使用sum()求和,使用avg函数求平均值
mysql> select id from user; +----+ | id | +----+ | 1 | | 3 | | 4 | +----+ 3 rows in set (0.00 sec) mysql> select sum(id) from user; +---------+ | sum(id) | +---------+ | 8 | +---------+ 1 row in set (0.00 sec) mysql> select avg(id) from user; +---------+ | avg(id) | +---------+ | 2.6667 | +---------+ 1 row in set (0.04 sec)
13,使用max()求最大值,使用min求最小值
mysql> select id from user; +----+ | id | +----+ | 1 | | 3 | | 4 | +----+ 3 rows in set (0.00 sec) mysql> select max(id) from user; +---------+ | max(id) | +---------+ | 4 | +---------+ 1 row in set (0.00 sec) mysql> select min(id) from user; +---------+ | min(id) | +---------+ | 1 | +---------+ 1 row in set (0.00 sec)
原文链接:http://soukenan.blog.51cto.com/5130995/1047201
以上是关于php中查询mysql select 第一个字段 第二个字段 from 表名。输出语句应的主要内容,如果未能解决你的问题,请参考以下文章
php增加一个字段后并按照前一个字段的数字按从小到大排序后填到里面