我想创建一个数据库查询字符串,应该是这样的,
(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] ) + ";
}
我知道上面的代码是完全错误的,实现代码以获得所需结果的正确方法是什么?
更新:
我想得到总数来自country_id而不是个人,可以存在于1到3个表中,以下查询似乎对我来说很好,但如果你有更好的解决方案,请告诉我< / p>
SELECT(
(SELECT COUNT(*) FROM states WHERE country_id IN(121,122)) +
(SELECT COUNT(*) FROM cities WHERE country_id IN(121,122) ) +
(SELECT COUNT(*) FROM areas WHERE country_id IN(121,122) )
);
答案 0 :(得分:5)
您可以使用JOIN:
执行此操作$id = array('121', '122');
jon_darkstar的数量(谢谢!):
"SELECT COUNT(DISTINCT(state_id)) + COUNT(DISTINCT(city_id)) + COUNT(DISTINCT(area_id)) as desired_sum
使用相同的cities
country_id
的表格
LEFT JOIN cities ON states.country_id = cities.country_id
使用相同的areas
country_id
的表格
LEFT JOIN areas ON states.country_id = areas.country_id
过滤您在数组中提供的country_id
的结果,使用名为implode()的php函数对其进行内嵌。
WHERE states.country_id IN (" . implode(', ', $id) . ")"
或者,在$cid
将成为你的country_id的foreach循环中(请参阅下面的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) . ")";
答案 1 :(得分:1)
试试这段代码:
<?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);
?>
但我强烈建议不要采用这种方法来获得你想要做的事情。
答案 2 :(得分:1)
注意到最后几条评论,试一试
$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
中指定的任何国家/地区内任何“单位”(城市,州或地区)的总数。它确实假设每个国家至少有一个州。 (即 - 如果国家X中有城市和区域但没有州,则不计算这些城市和地区)。还假设states
,cities
,areas
的主键是state_id
,city_id
和area_id
(不仅仅是id
)
答案 3 :(得分:0)
您可以减少查询次数,但我不认为尝试UNION
会更好。我的建议是改进这样的查询:
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"
}