首页 > PHP教程 > php开发知识文章

yii 操作mysql数据库函数

mysql登录

命令行: 

#最全的mysql登录方式:

mysql -hIP -uUSENME -pPWD -PPORT DBNAME -e "sql"  (-h表示数据库服务器IP,-u表示数据库用户名,-p表示数据库密码)

show databases  显示所有数据库

use databases  使用数据库

show tables  显示数据库所有表

 

图形界面工具

MySQL Workbench

 

mysql crud操作

insert (replace):增加

insert into group_contact_info (group_id,name,pinyin,created_time) values  (1,’小红’,’xiao’,14087657678),(2,’小红2’,’xiao’,14087657678);

update:更新

update group_contact_info set password = ‘1', remark = ‘a'    where id = 1;

select:查询

select id from member where name = ‘a’

delete:删除

delete from member where id = 1

 

mysql 连接查询

左连接:left join

以左表为准,去右表找数据,如果没有匹配的数据,则以null补空位,所以输出结果数>=左表原数据数

右连接:right join

a left join b 等价于 b right join a  推荐使用左连接代替右连接 

内连接: inner join

查询结果是左右连接的交集,左右连接的结果去除null项后的并集(去除了重复项)

select a.id aid,b.id bid b.poster, from group_contact_phone a left join member b on a.phone=b.phone where a.contact_info_id in (1,2)"

子查询

 where(条件查询)、having(筛选)、group by(分组)、order by(排序)、limit(限制结果数)

 where常用运算符:

    比较运算符

        > ,  < ,=  , != (< >),>=   ,   <=  

        in(v1,v2..vn)  

        between v1 and v2    在v1至v2之间(包含v1,v2)

    逻辑运算符

        not ( ! )  逻辑非

        or ( || )    逻辑或

        and ( && )  逻辑与 

        where price>=1 and price <= 6 or price >=5and price <=7

        取5-7或者1-6的值

        where price not between 1and 6

        不在1与6之间的值

 

    模糊查询

        like 像

        通配符:

        %  任意字符

        _   单个字符

            where goods_name like 'php%'

            where goods_name like 'php__'

 

 group by 分组

    一般情况下group需与统计函数(聚合函数)一起使用才有意义

    如:select goods_id,goods_name,cat_id,max(shop_price) from goods group by cat_id;

            这里取出来的结果中的good_name是错误的!因为shop_price使用了max函数,那么它是取最大的,而语句中使用了group by 分组,那么goods_name并没有使用聚合函数,它只是cat_id下的第一个商品,并不会因为shop_price改变而改变

    mysql中的五种统计函数:

    (1)max:求最大值

        select max(goods_price) from goods

          这里会取出最大的价格的值,只有值

            #查询每个栏目下价格最高的

            select cat_id,max(goods_price) from goos group by cat_id;

            #查出价格最高的商品编号

            select goods_id,max(goods_price) from goods group by goods_id;       

    (2)min:求最小值

    (3)sum:求总数和

            #求商品库存总和

            select sum(goods_number) from goods;

    (4)avg:求平均值

            #求每个栏目的商品平均价格

            select cat_id,avg(goods_price) from goods group by cat_id;

    (5)count:求总行数

            #求每个栏目下商品种类

            select cat_id,count(*) from goods group by cat_id;

       ###要把每个字段名当成变量来理解,它可以进行运算###

            例:查询本店每个商品价格比市场价低多少;

            select goods_id,goods_name,goods_price-market_price from goods;

                查询每个栏目下面积压的货款

            select cat_id,sum(goods_price*goods_number) from goods group by cat_id; 

        ###可以用as来给计算结果取个别名###

            select cat_id,sum(goods_price * goods_number)  as hk from goods group by cat_id

            不仅列名可以取别名,表单也可以取别名

 

mysql yii函数

增加

   $group_u = new GroupContact();

   $group_u->group_name = $val;

   $group_u->save() $group_u->getErrors()

查询

   $group = GroupContact::model()->find("id = {$groupid}");

   findAll(),findByPk()

更新

  $group->update()

删除

  $group->delete()

执行sql语句

  $connection = Yii::app()->db;

  $sql = select  insert update delete

  $command = $connection->createCommand($sql);

  $result = $command->queryAll() execute()

Yii中CDbCriteria查询条件

$criteria = new CDbCriteria;

$criteria->select = 'id,group_id,name';

$criteria->addInCondition('group_id', $group_id);

$find_result = GroupContactInfo::model()->findAll($criteria);

$criteria->addCondition("id=1"); //查询条件,即where id = 1        

$criteria->addInCondition('id', array(1,2,3,4,5)); //代表where id IN (1,2,3,4,5);        

$criteria->addNotInCondition('id', array(1,2,3,4,5));//与上面正好相法,是NOT IN        

$criteria->addCondition('id=1','OR');//这是OR条件,多个条件的时候,该条件是OR而非AND       

$criteria->addSearchCondition('link', 'php');//搜索条件,表示:where name like '%php%'        

$criteria->addBetweenCondition('id', 1, 5);//between 1 and 5        

$criteria->compare('id', 1);    //这个方法比较特殊,他会根据你的参数自动处理成addCondition或者addInCondition,如果第二个参数是数组就会调用addInCondition      

 /**   

 * 传递变量   

 */      

$criteria->addCondition("id = :id");      

$criteria->params[':id']=10;      

/**   

* 一些public vars   

*/      

$criteria->select = 'id,type'; //代表了要查询的字段,默认select='*';        

$criteria->join = 'xxx'; //连接表        

$criteria->with = 'xxx'; //调用relations         

$criteria->limit = 10;    //取1条数据,如果小于0,则不作处理       

$criteria->offset = 1;   //两条合并起来,则表示 limit 10 offset 1,或者代表了。limit 1,10       

$criteria->order = 'id DESC' ;//排序条件        

$criteria->group = 'group type';        

$criteria->having = 'having 条件 ';        

$criteria->distinct = FALSE; //是否唯一查询

 

参数绑定

为了避免 SQL 注入攻击 和改善执行反复的SQL语句的性能, 你可以"prepare" 一个 SQL 语句,其中的可选参数占位符在参数绑定过程中被实际的数据代替.

调用 CDbCommand::bindParam() 或 CDbCommand::bindValue() 以替换这些占位符为实际的参数. 参数无需以引号环绕: 底层数据库驱动为你完成. 参数绑定必须在 SQL 语句被执行前完成

关闭
感谢您的支持,我会继续努力!
扫码打赏,建议金额1-10元


提醒:打赏金额将直接进入对方账号,无法退款,请您谨慎操作。