Php 使用循环将此数组转换为字符串的逻辑

Php 使用循环将此数组转换为字符串的逻辑,php,mysql,Php,Mysql,我想创建一个数据库查询字符串,如下所示 (SELECT COUNT(*) FROM states WHERE country_id = 121 ) + (SELECT COUNT(*) FROM cities WHERE country_id = 121 ) + (SELECT COUNT(*) FROM areas WHERE country_id = 121) + (SELECT COUNT(*) FROM states WHERE country_id = 122) + (SELE

我想创建一个数据库查询字符串,如下所示

(SELECT COUNT(*) FROM states WHERE country_id = 121 ) + 
(SELECT COUNT(*) FROM cities WHERE country_id = 121 ) + 
(SELECT COUNT(*) FROM areas WHERE country_id = 121) + 
(SELECT COUNT(*) FROM states WHERE country_id = 122) + 
(SELECT COUNT(*) FROM cities WHERE country_id = 122) + 
(SELECT COUNT(*) FROM areas WHERE country_id = 122)
为此,我使用了这样的代码

$id = array('121','122');
$table = array('states','cities','areas');
$loopCount = count($id) * count($table);
$queryString = array();
for($i=0;$i<$loopCount;$i++)
{
    $queryString[] = "(SELECT COUNT(*) FROM $table[$i] WHERE country_id = $id[$i] ) + ";
}
您可以通过以下方式完成:

伯爵,谢谢你

"SELECT COUNT(DISTINCT(state_id)) + COUNT(DISTINCT(city_id)) + COUNT(DISTINCT(area_id)) as desired_sum
加入另一个名为“具有相同国家/地区id的城市”的表

使用相同的国家/地区id连接另一个名为areas的表

为数组中提供的国家/地区id筛选结果,并使用名为的php函数对其进行内爆

或者,在foreach循环中,$cid将是您的国家/地区,请参见下面jon_darkstar的评论

WHERE states.country_id = " . $cid . ""
单个查询:

"SELECT * FROM states, 
        COUNT(DISTINCT(state_id)) + 
        COUNT(DISTINCT(city_id)) + 
        COUNT(DISTINCT(area_id)) AS desired_sum
        JOIN `cities` ON states.country_id = cities.country_id
        JOIN `areas` ON states.country_id = areas.country_id
        WHERE states.country_id IN (" . implode(', ', $id) . ")";
请尝试以下代码:

<?php
$a=array();
$a[]=array('states','121');
$a[]=array('cities','121');
$a[]=array('areas','121');
$a[]=array('states','122');
$a[]=array('cities','122');
$a[]=array('areas','122');

$r=array();
foreach($a as $v)
    $r[]='(SELECT COUNT(*) FROM '.$v[0].' WHERE country_id = '.$v[1].' )';
$r=implode(' + ',$r);
?>

但是我强烈建议不要使用这种方法来获得你想要做的事情。

你可以减少查询的数量,但我不认为尝试联合会更好。我的建议是改进如下查询:

SELECT COUNT(*) AS number FROM states WHERE country_id IN (121, 122) GROUP BY country_id
现在每个表有一个查询。 在php方面,您可以这样做:

$id = array('121','122');
$table = array('states','cities','areas');
foreach($table as $tableName) {
    $queryString[] = "SELECT COUNT(*) AS number FROM {$tableName} WHERE country_id IN (". implode(',', $id) .") GROUP BY country_id"
}

刚刚注意到最后几条评论,试试看

$countryCodes = array(121, 122);
$countryCodeString = implode($countryCodes, ', ');

$sql = 
  "SELECT COUNT(DISTINCT(S.id)) + COUNT(DISTINCT(C.id)) + COUNT(DISTINCT(A.id)) as desired_sum 
   FROM states S
        JOIN cities C ON S.country_id = C.country_id
        JOIN areas A ON A.country_id = S.country_id
   WHERE s.country_id in ($countryCodes)";

$res = mysql_query($sql);
$arr = mysql_fetch_assoc($res);
$val = $arr['desired_sum'];

因此,这将给出一个整数,即$countryCodes中指定的任何国家/地区内任何“单位”城市、州或地区的总数。它确实假设每个国家至少有一个州。ie-如果X国境内有城市和地区,但没有州,这些城市和地区将不被计算在内。还假设州、城市、地区的主键是state\u id、city\u id和area\u id而不仅仅是id

问题是我想动态执行,因为$id可以有多个值,并且尽可能多,而$value可以有1到3个值。为了避免不必要的强制转换,您可能需要删除121和122附近的引号,因为这是一个数字字段。刚刚更新了我的代码段,以便您可以动态使用许多变量。@fabrik我无法理解您的代码,如果您能尝试解释一下该查询的情况,我将非常高兴。抱歉,但我仍在学习:@Ibrahim Azhar Armar也请阅读链接的MySQL文档页面,但我将尝试总结发生的事情。请参阅我的最新答案。这看起来很有趣,让我试着给您回复。请使用EXPLAIN对您的查询进行基准测试。如果这样做效率高,我会感到惊讶。这就是我所困惑的。主键只是id不是州id、城市id或地区id。。无论如何,谢谢我的测试,让你知道我的主键只是id,外键是国家id、州id、城市id、地区id等。当我使用你的代码时,它会给我这个错误。where子句中的未知列“states.country\u id”我使用了以下代码根据需要选择COUNTDISTINCTs.id+COUNTDISTINCTc.id+COUNTDISTINCTa.id从州加入城市c在s.country\u id=c.country\u id在a.country\u id=s.country\u id在118W中加入地区a在何处加入states.country\u id,非常感谢您,经过长时间的测试和耐心,我不仅理解了代码,而且让它出色地工作。谢谢你……:
"SELECT * FROM states, 
        COUNT(DISTINCT(state_id)) + 
        COUNT(DISTINCT(city_id)) + 
        COUNT(DISTINCT(area_id)) AS desired_sum
        JOIN `cities` ON states.country_id = cities.country_id
        JOIN `areas` ON states.country_id = areas.country_id
        WHERE states.country_id IN (" . implode(', ', $id) . ")";
<?php
$a=array();
$a[]=array('states','121');
$a[]=array('cities','121');
$a[]=array('areas','121');
$a[]=array('states','122');
$a[]=array('cities','122');
$a[]=array('areas','122');

$r=array();
foreach($a as $v)
    $r[]='(SELECT COUNT(*) FROM '.$v[0].' WHERE country_id = '.$v[1].' )';
$r=implode(' + ',$r);
?>
SELECT COUNT(*) AS number FROM states WHERE country_id IN (121, 122) GROUP BY country_id
$id = array('121','122');
$table = array('states','cities','areas');
foreach($table as $tableName) {
    $queryString[] = "SELECT COUNT(*) AS number FROM {$tableName} WHERE country_id IN (". implode(',', $id) .") GROUP BY country_id"
}
$countryCodes = array(121, 122);
$countryCodeString = implode($countryCodes, ', ');

$sql = 
  "SELECT COUNT(DISTINCT(S.id)) + COUNT(DISTINCT(C.id)) + COUNT(DISTINCT(A.id)) as desired_sum 
   FROM states S
        JOIN cities C ON S.country_id = C.country_id
        JOIN areas A ON A.country_id = S.country_id
   WHERE s.country_id in ($countryCodes)";

$res = mysql_query($sql);
$arr = mysql_fetch_assoc($res);
$val = $arr['desired_sum'];