2011-07-23 27 views
0

私はPHP/MYSQL検索モジュールを開発しています。ここでは、さまざまな条件に基づいてテーブルを検索しなければならず、いくつかのテーブルが11個あり、複数の結合を使って1つのMySQLクエリを作成しました。特定のレコードを検索するには、私が使用しているMYSQLクエリがあります。mysqlでレコードを検索するにはどうすればよいですか?

SELECT 
prop.id, 
prop.serial, 
prop.title, 
prop.rating, 
prop.addDate, 
prop.approve, 
prop.status, 
prop.user_id as userId, 
user_det.email as email, 
user_det.name as name, 
prop.area_id as areaId, 
area.name as areaName, 
area.zipCode as zipCode, 
area.city_id as cityId, 
city.name as cityName, 
city.state_id as stateId, 
state.name as stateName, 
state.country_id as countryId, 
country.name as countryName, 
prop.subCategory_id as subCategoryId, 
subCat.name as subCategoryName, 
subCat.category_id as categoryId, 
cat.name as categoryName, 
prop.transaction_id as transactionId, 
trans.name as transactionName, 
price.area as landArea, 
price.price as priceSqFt, 
price.total_price as totalPrice, 
features.bedroom, 
features.bathroom, 
features.balcony, 
features.furnished, 
features.floorNum, 
features.totalFloor 
FROM properties prop 
LEFT JOIN user_details user_det ON (prop.user_id = user_det.user_id) 
LEFT JOIN areas area ON (prop.area_id = area.id) 
LEFT JOIN cities city ON (area.city_id = city.id) 
LEFT JOIN states state ON (city.state_id = state.id) 
LEFT JOIN countries country ON (state.country_id = country.id) 
LEFT JOIN subCategories subCat ON (prop.subCategory_id = subCat.id) 
LEFT JOIN categories cat ON (subCat.category_id = cat.id) 
LEFT JOIN transactions trans ON (prop.transaction_id = trans.id) 
LEFT JOIN prop_prices price ON (price.property_id = prop.id) 
LEFT JOIN prop_features features ON (features.property_id = prop.id) 

すべてがここにうまく動作しますが、私は下の表は、prop_amenitiesは、この表の内容ですと呼ばれている状況があります。テーブルとして

enter image description here

私はそれを使用して照会する場合は、上記の複数のproperty_idを持っている、主にそれは私が使用JOINの種類に応じて、他の人を省略重複レコードまたは単一のレコードを返します、その後合流します。だから代わりに私はこのように扱いたいと思う。

テーブルprop_amenitiesを使用して、結果を返さない条件のみを処理します。 たとえば、アメニティIDが1,5,9,17、および24のプロパティを検索している場合、すべてのレコードがprop_amenitiesテーブルに存在するかどうか、この場合は1,5,9,17および24かどうかを確認する必要があります。上記のすべての列を含む適切なレコードを戻します。

私は、この状況をMySQLを使って処理することについて断然です。どうやってこれをやるの?

は、あなたが、それはここでのキーワードだ。「すべてのレコードprop_amenities表にを存在するかどうかを確認」と述べた。..

答えて

2

をありがとう

SELECT ... 
FROM properties AS prop 
LEFT JOIN ... 
WHERE EXISTS (SELECT 1 FROM prop_amenities AS pa WHERE pa.property_id = prop.property_id AND pa.amenity_id = 7); 
関連する問題