请问php+mysql查询语句该如何写,查询表字段的总和,按每天且按类型字段
编号 商品 数量 类型 日期
-----------------------------------------------------
id title Number type time
----------------------------------------------------
1 手机型号1 90 手机 2012-08-14
-----------------------------------------------------
2 手机型号1 90 手机 2012-08-14
------------------------------------------------------
3 手机型号2 100 手机 2012-08-14
-------------------------------------------------------
4 配件型号1 100 配件 2012-08-14
-------------------------------------------------------
1、查询商品,当天类型为手机的数量的总和
比如:手机型号1:90+90=180
2、查询类型“手机”,当天的数量的总和
比如:手机类型:90+90+100=280 展开
(我把你表格中的数据 "中文" 换成了 "英文" 数据和你的是一致的)
我创建你的表格的SQL语句:
create table cellphone
( id int(3) zerofill not null auto_increment primary key,
title char(20) not null,
number int not null,
type char(20) not null,
time date not null
);
插入数据的SQL语句:
insert into cellphone (title,number,type,time) values
("Phone Model 1",90,"cellphone","2012-08-14"),
("Phone Model 1",90,"cellphone","2012-08-14"),
("Phone Model 2",100,"cellphone","2012-08-14"),
("Mobile Accessory 1",100,"Accessory","2012-08-14");
查询语句:
select title, sum(number), time
from cellphone
where type = 'cellphone'
and time= date_format(now(),'%Y-%m-%d') //获取今天日期的函数
group by title;
创建后的表:
查询结果:
php 实现代码:
<?php
/************************ MySQL连接 ************************/
$dbc = @ mysqli_connect('localhost', 'root', 'mysql', 'test');
if (mysqli_connect_errno()) {
echo "Error: Could not connect to database.Please try again later.";
exit;
}
/************************************************************/
/********************************** 执行查询 **********************************/
$query = "select title, sum(number), time
from cellphone
where type = 'cellphone'
and time= date_format(now(),'%Y-%m-%d')
group by title";
$results = mysqli_query($dbc, $query);
if ($results) {
echo mysqli_affected_rows($dbc)." rows get.<br/>";
} else {
echo "An error has occurred.";
}
/***********************************************************************************/
/********************************** 输出结果 **********************************/
while ( list($title, $num, $time) = mysqli_fetch_row($results) ) {
echo "$title $num $time<br/>";
}
/******************************************************************************/
/********************************* 关闭和释放 *********************************/
mysqli_free_result($results);
mysqli_close($dbc);
/******************************************************************************/
?>
运行结果:
辛苦了,如果我在现有的数据表中,用PHP用表单批量插入N条数据,应该怎么做!我再加分
最后一行是后来插入的:
(你可以多做几个表单,一次提交多条记录,时间问题我就没做那么详细)
(html代码写不了,提示输入过多,如需要,加我qq695565914,先评为满意答案在加哦)
php代码 (insert.php):
<?php
/********************* 表单数据处理 *********************/
$title = $_POST['title'];
$number = $_POST['number'];
$type = $_POST['type'];
$time = $_POST['time'];
if (!$title || !$number || !$type || !$time) {
echo "You have not entered all the required detail.<br/>"
."Please go back and try again.";
exit;
}
if (!get_magic_quotes_gpc()) {
$title = addslashes($title);
$number = intval($number);
$type = addslashes($type);
$time = addslashes($time);
}
/************************ MySQL连接 ************************/
$dbc = @ mysqli_connect('localhost', 'root', 'mysql', 'test');
if (mysqli_connect_errno()) {
echo "Error: Could not connect to database.Please try again later.";
exit;
}
/******************************** 用prepare执行查询 ********************************/
$query = "insert into cellphone (title,number,type,time) values (?, ?, ?, ?)";
$stmt = mysqli_stmt_init($dbc);
mysqli_stmt_prepare($stmt, $query);
mysqli_stmt_bind_param($stmt, "siss", $title, $number, $type, $time);
if ( mysqli_stmt_execute($stmt) ) {
echo mysqli_affected_rows($dbc)." rows inserted into database.";
echo "<a href='new_phone.html'>返回继续添加</a>";
} else {
echo "An error has occurred.The item was not added.";
}
/**************** 关闭 ****************/
$dbc -> close();
?>
select sum(number) from tablename where type="手机" and time="2012-08-14";
2.select sum(number) where time='2012-08-14' and type='手机';