开发者

nested select queries issue

开发者 https://www.devze.com 2023-03-04 17:18 出处:网络
I have a select query that fetches me a table with rows, I want to get a particular row from this result but using the same first query, I explain:

I have a select query that fetches me a table with rows, I want to get a particular row from this result but using the same first query, I explain:

We have a query:

$myQuery=("select col1,col2 from table where ..");

The seems is like that:

|col1|col2 |
|----------|
|res1|res11|
|res2|res21|
|res3|res31|

I want now to get res31, for example, but usi开发者_JAVA技巧ng the same last query called $myQuery, like we add a select * from ... to it.

I hope I did explain it, please say if I didn't enough.

Regards!


If I understood correctly: you can type just

SELECT * FROM ($myQuery) WHERE id=31 (OR any other stuff you wish to)

MySQL will understand that


If you use Mysqli, then you could use binded params to achieve this. PDO would be even better to use.

Psuedo-code for how you would then do this with binded params:

$myQuery = "SELECT col1, col2, FROM table WHERE col2 = ?";
$result_31 = $db->select($myQuery, Array('res31')); 
$result_32 = $db->select($myQuery, Array('res32')); 

This is just an example of binded your query param to the ?. Dig into the APIs of mysqli and PDO for which suits you best. PDO is highly recommended though as you could later change from using mysql to postgres if needed, without changing all of your DB queries.

0

精彩评论

暂无评论...
验证码 换一张
取 消