php可以查询先前查询的结果吗?

时间:2022-04-26 11:50:56

In some languages (ColdFusion comes to mind), you can run a query on the result set from a previous query. Is it possible to do something like that in php (with MySQL as the database)?

在某些语言中(想到ColdFusion),您可以对先前查询的结果集运行查询。是否有可能在php(用MySQL作为数据库)中做类似的事情?

I sort of want to do:

我有点想做:

$rs1 = do_query( "SELECT * FROM animals WHERE type = 'fish'" );
$rs2 = do_query( "SELECT * FROM rs1 WHERE name = 'trout'" );

5 个解决方案

#1


6  

There is no MySQL function like this for PHP, however there is a more advanced substitute for it.

对于PHP,没有像这样的MySQL函数,但是有一个更高级的替代品。

Edit: For those of you who don't know what a query of queries is, it's exactly this and there's a purpose some people do it like this. Using an AND operator is ****NOT**** the same thing! If I want results where username='animuson' for one part of my script and then want all the results out of that query where status='1', it is not logical for me to run another query using an AND operator, it is much more logical to loop through the previous results in PHP. Stop upvoting things without reading the comments on why they weren't upvoted in the first place, that's just lazy. If you don't have a clue what's being talked about, you shouldn't be upvoting or downvoting in the first place.

编辑:对于那些不知道查询查询是什么的人来说,就是这样,有一个人就是这样做的目的。使用AND运算符是**** NOT ****同样的事情!如果我想在我的脚本的一部分中使用username ='animuson'的结果然后想要那个状态='1'的查询中的所有结果,那么使用AND运算符运行另一个查询是不合逻辑的,它是在PHP中循环使用以前的结果更合乎逻辑。在没有阅读关于他们为什么不首先被投票的评论的情况下停止提升事物,那只是懒惰。如果您不知道所谈论的是什么,那么您首先不应该支持或低估。

#2


2  

Well, you may want to do this without touching the db:

好吧,您可能希望在不触及数据库的情况下执行此操作:

while($t = mysql_fetch_array($rs1)){
    if($t[name] == 'trout'){ 
        echo 'This is the one we\'re looking for!'; 
        break;
    }
}

#3


1  

In PHP, it would be terribly inefficient. You would have to loop through each row and check that its name was trout. However, is there any reason you can't do

在PHP中,它会非常低效。您必须遍历每一行并检查其名称是否为鳟鱼。但是,有什么理由你做不到

SELECT * FROM `animals` WHERE `type` = 'fish' AND `name` = 'trout'

in SQL? It would be much, much faster.

在SQL?它会快得多。

#4


1  

You can also do something like

你也可以这样做

select morestuff from (select stuff from table where a = b ) where c = d;

#5


0  

Use the AND keyword?

使用AND关键字?

"SELECT * FROM animals WHERE type = 'fish' and name='trout'"

Also, you can use LINQ for php http://phplinq.codeplex.com/

另外,你可以使用LINQ for php http://phplinq.codeplex.com/

#1


6  

There is no MySQL function like this for PHP, however there is a more advanced substitute for it.

对于PHP,没有像这样的MySQL函数,但是有一个更高级的替代品。

Edit: For those of you who don't know what a query of queries is, it's exactly this and there's a purpose some people do it like this. Using an AND operator is ****NOT**** the same thing! If I want results where username='animuson' for one part of my script and then want all the results out of that query where status='1', it is not logical for me to run another query using an AND operator, it is much more logical to loop through the previous results in PHP. Stop upvoting things without reading the comments on why they weren't upvoted in the first place, that's just lazy. If you don't have a clue what's being talked about, you shouldn't be upvoting or downvoting in the first place.

编辑:对于那些不知道查询查询是什么的人来说,就是这样,有一个人就是这样做的目的。使用AND运算符是**** NOT ****同样的事情!如果我想在我的脚本的一部分中使用username ='animuson'的结果然后想要那个状态='1'的查询中的所有结果,那么使用AND运算符运行另一个查询是不合逻辑的,它是在PHP中循环使用以前的结果更合乎逻辑。在没有阅读关于他们为什么不首先被投票的评论的情况下停止提升事物,那只是懒惰。如果您不知道所谈论的是什么,那么您首先不应该支持或低估。

#2


2  

Well, you may want to do this without touching the db:

好吧,您可能希望在不触及数据库的情况下执行此操作:

while($t = mysql_fetch_array($rs1)){
    if($t[name] == 'trout'){ 
        echo 'This is the one we\'re looking for!'; 
        break;
    }
}

#3


1  

In PHP, it would be terribly inefficient. You would have to loop through each row and check that its name was trout. However, is there any reason you can't do

在PHP中,它会非常低效。您必须遍历每一行并检查其名称是否为鳟鱼。但是,有什么理由你做不到

SELECT * FROM `animals` WHERE `type` = 'fish' AND `name` = 'trout'

in SQL? It would be much, much faster.

在SQL?它会快得多。

#4


1  

You can also do something like

你也可以这样做

select morestuff from (select stuff from table where a = b ) where c = d;

#5


0  

Use the AND keyword?

使用AND关键字?

"SELECT * FROM animals WHERE type = 'fish' and name='trout'"

Also, you can use LINQ for php http://phplinq.codeplex.com/

另外,你可以使用LINQ for php http://phplinq.codeplex.com/