查询查询

编程入门 行业动态 更新时间:2024-10-27 22:34:09
本文介绍了查询查询-MySQL和PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近正在尝试做一个项目*,这使我提出了这个问题.尽管从那时起我已经找到了替代解决方案,但我仍然对我所设想的以任何方式实现的可能性感到好奇.

本质上,我想知道是否仍然可以对php中的MySQL查询结果执行MySQL查询.例如:

$result = mysql_query("SELECT * FROM foo WHERE bar=".$barValue);

然后,可以在$result上执行多个查询:

AND THEN, be able to perform multiple queries on $result:

$newResult = mysql_query("SELECT * FROM $result WHERE otherBar=".$barValue);

OR

$otherNewResult = mysql_query("SELECT * FROM $result WHERE otherOtherBar=".$barValue." ORDER BY foobar ASC");

依此类推...

我意识到我可以在原始查询中附加新的WHERE语句和ORDER BY,但这会导致我不必要地查询数据库,这使我无法编写面向对象的对象代码(因为我无法传递要查询的结果,而不得不在每个函数中重新查询数据库...)

I realize that I could append the original query with my new WHERE statements and ORDER BYs, but that causes my to query the database unnecessarily and it prevents me from writing more objected oriented code (because I can't pass around a result to be queried, but rather have to requery the database in every function...)

赞赏任何建议,代码段,框架或杂乱无章.

*顺便说一句,我的项目是要查询一个大型数据库,查找某些年龄段的人,然后再查询这些年龄段的人口统计信息.

*BTW, my project was having to query a large database of people for people born in certain age groups and then query those age groups for different demographics.

不,编写自定义函数来查询数据库不值得它给我带来面向对象(和可修改性)的作用

No, writing a custom function to query the database is not worth the object-orientation (and modifiability) it would give me

推荐答案

您可以在同一SQL查询中执行嵌套查询,并使PHP脱离它:

You could do a nested query in the same SQL query and keep PHP out of it:

'SELECT * FROM (SELECT * FROM foo WHERE bar="something") AS q1 WHERE q1.bar2 = "something else"'

更多推荐

查询查询

本文发布于:2023-10-27 02:41:04,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1532064.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!