您的位置:首页 > 数据库 > MySQL

MYSQL 中的常见问题(包括数据筛选)

2016-04-10 16:28 411 查看
PHP页面编码问题

PHP无法返回汉字,返回相应的汉字数据时出现乱码,这时只需要在代码开始处加入相应的代码。

php页面为utf编码
header("Content-type: text/html; charset=utf-8");

php页面为gbk编码
header("Content-type: text/html; charset=gb2312");

php页面为big5编码
header("Content-type: text/html; charset=big5");


数据不重复筛选—distinct用法和count函数

distinct关键字过滤掉多余的重复记录只保留一条,但往往只用它来返回不重复记录的条数,而不是用它来返回不重记录的所有值。

用count函数语句可以实现返回:

select *, count(distinct name) from table group by name

group by 必须放在 order by 和 limit之前,不然会报错

示例代码:

<?php
header("Content-type: text/html; charset=utf-8");

$con = mysql_connect("localhost","root","huweishen");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("home_server", $con);
$result = mysql_query("SELECT *,count(distinct Device) FROM message group by Device" );

while($row = mysql_fetch_array($result))
{
echo $row[Device];
}
mysql_close($con);
?>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: