【问题标题】:How do i deal with this situation for searching records in mysql?我该如何处理在mysql中搜索记录的这种情况?
【发布时间】:2011-07-23 12:06:38
【问题描述】:

我正在开发一个 PHP/MYSQL 搜索模块,我必须根据许多不同的条件搜索表,我有大约 11 个表,并且我使用多个连接来创建一个 MySQL 查询并基于我打算的 WHERE 子句要搜索特定记录,这是我正在使用的 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 的表,下面是该表的内容。

由于上表有多个property_id,如果我使用JOINS 查询它,那么根据我使用的JOIN 的类型,大多数情况下它将返回重复记录或省略其他记录的单个记录。所以我想这样处理。

使用表prop_amenities 只处理不返回结果的条件。 例如,我正在搜索设施 ID 为 1、5、9、17 和 24 的属性,那么它应该检查 prop_amenities 表中是否存在所有记录,即在这种情况下为 1、5、9、17 和 24 .并返回所有上述选定列的相应记录。

我对使用 MySQL 处理这种情况一无所知。我该怎么做?

谢谢你..

【问题讨论】:

    标签: mysql sql database


    【解决方案1】:

    您说“检查prop_amenities 表中的所有记录是否存在”,这就是这里的关键词。

    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);
    

    【讨论】:

      猜你喜欢
      • 2012-12-16
      • 2020-04-05
      • 1970-01-01
      • 1970-01-01
      • 2016-06-22
      • 2012-01-04
      • 2014-05-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多