sql语句及 quoteInto()

本文介绍了SQL语句的执行方式、多表连接、聚集函数的应用等实用技巧,并详细解析了QuteInto方法在不同场景下的参数绑定实践。
部署运行你感兴趣的模型镜像

1. sql语句是从右往左执行的。

eg. select * from stu,credit; 把credit看做主表

多表连接

查询女生少于200人的系

select count(*) girls,sdept from student where ssex='F' group by sdept having girls<200


select  t1.*,c.cname from course c,(select count(*),cid from studcourse where grade<60 group by cid) t1 where t1.cid=c.cid

course c,c 是course的别名,(select count(*),cid from studcourse where grade<60 group by cid) 是暂时的表,起别名为t1,查找t1的总共人数,及科目序号cid,以及c.cname;

若是只需要查t1表中的count(*),而不需要cid,则需要给count(*)起别名,

select  t1.nums,c.cname from course c,(select count(*) nums,cid from studcourse where grade<60 group by cid) t1 where t1.cid=c.cid

聚集函数

select count(*),sdept from student where sdept="计算机系" 此语句错误

当一个是聚集函数,一个变化的值时,必须后面要用分组,即group by

正确:select count(*),"计算机系" from student where sdept="计算机系"

select count(*) sdept from student group by sdept

2. quteInto() 参数绑定 

可读性好,防注入,性能提高

单条件+查询全部字段  二维数组 需要toArray()

$db = $tableModel->getAdapter();

$where = $db->quoteInto("id=?","10");

$order="";//按什么排序  $count="";//显示多少,$offset="";//从哪里开始显示

$res = $tableModel->fetchAll($where,$order,$count,$offset)->toArray();


多条件+全部字段  二维数组 需要toArray()

$where = $db->quoteInto("id=?","10").$db->quoteInto("AND xx=?","**");

$order....

$res = $tableModel->fetchAll($where,$order,$count,$offset)->toArray();


单条件+部分字段 一维数组 不需要toArray()

$sql=$db->quoteInto("select a,b from table where id=?","1");

$res = $db->query($sql)->fetchAll();


多条件+部分字段 一维数组 不需要toArray()

$res = $db->query("select a,c  from table where a=:a and b=:b",array('a'=>5,'b'=>7))->fetchAll();








您可能感兴趣的与本文相关的镜像

Stable-Diffusion-3.5

Stable-Diffusion-3.5

图片生成
Stable-Diffusion

Stable Diffusion 3.5 (SD 3.5) 是由 Stability AI 推出的新一代文本到图像生成模型,相比 3.0 版本,它提升了图像质量、运行速度和硬件效率

评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值