Re: MySQL/PHP how to put the results of one query in another query
| From: | Ross Honniball | Date: | Sun, 26 Jun 2005 12:49:35 +0000 |
| Subject: | Re: MySQL/PHP how to put the results of one query in another query | ||
| References: | 1 | Groups: | php.db |
| Request: | Send a blank email to php-db+get-39771@lists.php.net to get a copy of this message | ||
This is what I use when needing to do this:
1. Put all the values you want to search for in an array $vals (loop around the first result)
2. $query = "
select count(Nome) from Aeromodelistas
where year(AnoQuota)=2005
and Sexo='Masculino' and Distrito IN ".sqlIn($vals)
;
function sqlIn($vals, $quote="'") {
if (!is_array($vals)) $vals = array($vals);
$s = '(';
$c = '';
foreach ($vals as $val) {
$s .= $c.$quote.addslashes($val).$quote;
$c = ',';
}
return $s.')';
}
Pedro Quaresma de Almeida wrote:
Hi I have two MySQL databases, on for aeromodelistas (aeromodelling) and another for Códigos Postais (Postal Codes). I whant to do the following query SELECT CódigoPostal FROM Aeromodelistas WHERE CódigoPostal IN (SELECT distinct(CP4) FROM codigopostal.LOCART,codigopostal.DISTRITO WHERE codigopostal.LOCART.DD=codigopostal.DISTRITO.DD AND codigopostal.DISTRITO.DESIG='Coimbra'); This query is not working, and I do not know why. If I try the two queries individualy they work, togheter they don't!? But the question I want to put to the members of this list is the following. Is it possible to do the following? // first do the subquery $sql_CP4s = "select distinct(CP4) from codigopostal.LOCART,codigopostal.DISTRITO where codigopostal.LOCART.DD=codigopostal.DISTRITO.DD and codigopostal.DISTRITO.DESIG='$nomeDistrito'"; $resultado_CP4s = mysql_query($sql_CP4s,$ligacao); $linha_CP4s = mysql_fetch_assoc($resultado_CP4s); // then use it's results in the main query $sql_Aero_Dist_Masc = "select count(Nome) from Aeromodelistas where year(AnoQuota)=2005 and Sexo='Masculino' and Distrito IN $linha_CP4s"; How can we include the results (not just one) of one query in another query? Is it possible?