Nadjib Mami
Nadjib Mami

Reputation: 5830

nested select queries issue

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 using 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!

Upvotes: 0

Views: 339

Answers (2)

Bryan Drewery
Bryan Drewery

Reputation: 2636

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.

Upvotes: 0

Corkscreewe
Corkscreewe

Reputation: 2961

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

Upvotes: 2

Related Questions