Can we add 2 different mysql_query results together?

Hi,

Is there anyway to add the value of 2 different mysql_query results together?
And no UNION will not work here, since the LIMIT of data to fetch in case of the 2nd Sql SELECT depends on number of data that was found via the 1st SELECT.

Here is the situation:

$to_find = 15;

$q1 = mysql_query($sq1); 

$count_1 = mysql_num_rows($q1);

$limit2  = $to_find - $count_1;

$sq2 = "SELECT id FROM test WHERE x = 'this' ORDER BY id DESC LIMIT $limit2";
$q1 = mysql_query($sq2); 

So is there a way now to add $q1 with $q2 so that in the resulting

while ($result_top_qs = mysql_fetch_array($qresult)) {

	//we will end up with the exact 15 results
}

Regards,
Dean

What’s in $q1 ? It may be that the query can be written as a single query, if we could see it.

Also note the old-style mysql_query and related functions need to be replaced with mysqli or PDO to continue working in PHP 7.

1 Like

Hi,

No, as I said there is NO Way that the Query can be written in a single Query since what we SELECT in the 2nd Query depends on how many results where returned via the 1st Query.

And thanks for the heads up that: “mysql_query and related functions need to be replaced with mysqli or PDO to continue working in PHP 7”

Presuming that you want to retrieve rows from both queries, one way might be to just stick them in an array to make the next part slightly easier, either with loops or with fetchAll() in PDO. I’m assuming that the first query needs to return x number of records, and the second will return 15-x in this example, not that the first query is just to decide what x would be, assuming that you re-use $q1 for the second result set just to simplify the post, rather than because the results are no longer required.

Not exactly, you posted

Which could very well be correct.
But it is highly possible that using a JOIN would work.

As previously posted, we need to see both queries to assess the possibility and hopefully come up with a single query.

Yes, we want to retrieve rows from both queries. So the number of rows to retrieve from query 2 depends on the number of rows that were retrieved from query 1. That is why UNION or JOIN will not work since we have NO idea how many rows would be retrieved from query 1 to then determine the number of rows to retrieve from query 2.

I guess from these replies, there is no built in Php or MySQL Command for addressing this, that is to post query join 2 rows results together, and what we need to do is to stick these results into an Array.

Maybe you have no idea. But a crafted query would very likely work.
Of course if you never post both queries you’ll never know that either.

2 Likes

I found a similar question with a quick search on “php merge two results”, where the OP wants to retrieve some records, then retrieve some more from the same table to make sure they always get four results. Without knowing how similar the queries are, it’s hard to know if it would help : http://stackoverflow.com/questions/13942347/combining-two-query-results-php-mysql

It sounds very similar to the original question, but without knowing the queries it’s impossible to say. It does kind of illustrate that the two result sets cannot be merged, so if you’re sure that the query cannot be done as a single one then the answer to your original question seems to be “no”.

Ok, Thanks.

This is not what my question is about. But thank you anyway.

FYI, I have found a work around to this issue.
FYI2: I was hoping that there would be a Php command that would join two MySQL queries together, which you would think by now in 2016 would be the case, but it is not. Oh well. So I had to take care off with moving the data to an Array and then joining the Arrays together.

Don’t blame the tools…

You just need a better understanding of php and mysql.

using two queries isn’t necessarily a poor solution. Is the solution with two queries scalable? If it is than I wouldn’t think much more about it.

2 Likes

Also, the very first thing you were asked was:-

Since you never disclosed that information, no one had a chance to offer you a working solution.
If you need to combine 2 queries, you need to know what both of them are.

1 Like

Did you mean that you want something to join the two queries together, or to join the two result sets together, so you could call fetch() and work through both result sets in sequence?

Maybe looking for
array_merge() ?
http://php.net/manual/en/function.array-merge.php

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.