查询查询-MySQL和PHP [英] Query a Query - MySQL and PHP

查看:81
本文介绍了查询查询-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"'

这篇关于查询查询-MySQL和PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆