$result1 = mysql_query("SELECT distinct city FROM postcodedb WHERE web='ALR' ORDER BY city") or die(mysql_error());
while($row1=mysql_fetch_array($result1)) {
$city = $row1['city'];
$result = mysql_query("SELECT * FROM postcodedb WHERE area='$city' AND city='$city' LIMIT 1") or die(mysql_error());
while($row=mysql_fetch_array($result)) {
$city = $row['city'];
}
}
所以在数据库中记录看起来像这样:
---------------------
id city area
---------------------
1 Bromley Bromley <- I need this record
2 Bromley other1
3 Bromley other2
4 Ilford Ilford <- and then this one
5 Ilford other1
---------------------
并且还有更多的城市和分配给这些城市的地区。
所以,我想要得到的是不同城市的列表,但对我来说重要的是我得到了两个城市的行ID
和区域
是一样的吗?
这个查询按照我想要的方式工作,但有点慢,我相信有更好的方法来实现相同的结果?
我希望我能清楚地解释自己。
提前非常感谢您!
妮塔
最佳答案
如果城市和区域相同,那么这应该是查询中的条件:
SELECT distinct city
FROM postcodedb
WHERE web = 'ALR' and city = area
ORDER BY city;
理想情况下,不会有重复项,因此您可以消除不同
:
SELECT city
FROM postcodedb
WHERE web = 'ALR' and city = area
ORDER BY city;
对于此查询,您需要在 postcodedb(web, city,area)
上建立索引以获得最佳性能。
关于php - 我想加快 MySQL 查询速度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27891871/