zoukankan      html  css  js  c++  java
  • mysql基础原生sql教程

    本文转自:https://www.cnblogs.com/shuguoqing/p/5796998.html

    数据库的链接和选择及编码 
    $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error());    // 连接数据库
    $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error());                        // 选择数据库
    mysql_query("set names 'utf8'");                                                                               // 设置数据库编码

    添加数据
    $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error());
    $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error());
    mysql_query("set names 'utf8'",$sel);
    $add="insert into title(title,content,username,time) values('$title','$content','$username',$time)";
    $query=mysql_query($add);
    if($query){
    echo "add sucess";
    echo "<meta http-equiv="refresh" content="1;URL=show_message.php" />";
    }
    else echo "add false";

    删除数据
    $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error());
    $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error());
    mysql_query("set names 'utf8'");
    $id=$_GET['id'];
    $delete="delete from title where id='$id'";
    $query=mysql_query($delete);
    if($query){
    echo "delete sucess!";
    echo "<meta http-equiv="refresh" content="1;URL=show_message.php" />";
    }
    else echo "delete false";

    改数据
    $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error());
    $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error());
    mysql_query("set names 'utf8'",$sel);
    $update="update title set title='".$title."',content='".$content."'
    where id='$id'";
    $query=mysql_query($update);
    if($query){
    echo "reset sucess!";
    echo "<meta http-equiv="refresh" content="1;URL=show_message.php" />";
    }
    else echo "reset false";

    查数据
    $link=mysql_connect("localhost","root","123456") or die("数据库连接失败".mysql_error());
    $sel=mysql_select_db("login",$link) or die("数据库选择失败".mysql_error());
    mysql_query("set names 'utf8'",$sel);
    $sql="select * from 表名 limit 10";
    $result=mysql_query($sql);
    while($arr=mysql_fetch_array($result, MYSQL_ASSOC)) {
         $rs[] = $arr;               // 将所有数据放到二位数组中 
         或 $arr = mysql_fetch_array();         // 取一条


    1. select查询语句和条件语句
       select 查询字段 from 表名 where 条件
       查询字段:可以使用通配符*、字段名、字段别名(字段名 as ..)
       表名:数据库.表名,表名
       常用条件:=等于、<>不等于、in(值,值)包含、not in(值,值)不包含、like匹配
       between在范围、not between不在范围、<、>
       条件预算:and、or、()

    2. select 查询字段 from 表名 where 条件
      (group by字段 分组)
      (order by字段,字段asc/desc 排序)
      (limit初始值,结束值 指针查询)
     
    3. select 计算 from 表名 where 条件
        count(*)统计函数
        max(*)最大值函数
        min(*)最小值函数
        avg(*)平均值
        sum(*)累加值函数

    4.模糊查询(%代表0个或多个字符,_代表1个字符。like比较耗时间,尽量少用) 

    like用法:
    SELECT * FROM 表名 WHERE username LIKE “$username%”;
    SELECT * FROM 表名 WHERE username LIKE “%$username%”;

    in用法:
    SELECT * FROM 表名 WHERE id in(1, 2, 5, 9);

  • 相关阅读:
    React
    移动端
    Flask 框架小记
    PyTorch 学习
    CNN 小结
    Django 环境下常用的模型设计
    Linux用户和用户组
    Linux下查看进程的命令输出的内容解释
    linux下配置tomcat开机自启动
    商业智能概述
  • 原文地址:https://www.cnblogs.com/cnn2017/p/11369455.html
Copyright © 2011-2022 走看看