帮我优化这个查询。请

时间:2011-04-07 15:17:38

标签: mysql query-optimization

$query_index_neighborhood1 = 
"SELECT areas_db.areas_name, areas_db.areas_id, neighborhoods_db.neighborhoods_id,
neighborhoods_db.neighborhoods_name, neighborhoods_db.neighborhoods_area_id, 
areas_db.areas_state_id 
FROM (
       (
         (
            restaurants_db 
            INNER JOIN neighborhoods_db ON neighborhoods_db.neighborhoods_id=restaurants_db.restaurants_neighborhood
         ) 
         INNER JOIN areas_db ON areas_db.areas_id=neighborhoods_db.neighborhoods_area_id
       )
       INNER JOIN areas_db AS areas_db1 on areas_db1.areas_id=restaurants_db.restaurants_area
    )
WHERE areas_db.areas_state_id=$mxstateid
GROUP BY neighborhoods_db.neighborhoods_id
ORDER BY areas_db.areas_id, neighborhoods_db.neighborhoods_name ASC";

1 个答案:

答案 0 :(得分:0)

作为一项有趣的思考练习,我想出了以下内容:

SELECT a.areas_name,
    a.areas_id,
    n.neighborhoods_id,
    n.neighborhoods_name,
    n.neighborhoods_area_id, 
    a.areas_state_id 
FROM neighborhoods_db AS n
INNER JOIN areas_db AS a ON a.areas_id = n.neighborhoods_area_id
WHERE a.areas_state_id = $mxstateid
    AND n.neighborhoods_id in (SELECT restaurants_neighborhood FROM restaurants_db)
ORDER BY a.areas_id, n.neighborhoods_name ASC

此外,表别名是你的朋友。