gpt4 book ai didi

mysql - 在mysql中查找记录时出现这种情况该如何处理?

转载 作者:行者123 更新时间:2023-11-29 09:06:54 25 4
gpt4 key购买 nike

我正在开发一个 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 的表,下面是该表的内容。

enter image description here

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

使用表prop_amenities只处理条件,不返回结果。例如,我正在搜索设施 ID 为 1,5,9,17 和 24 的特性,那么它应该检查 prop_amenities 表中是否存在所有记录,即 1,5,9,17在本例中为 24。并返回包含上述所有选定列的适当记录。

我对使用 MySQL 处理这种情况一无所知。我该如何继续?

谢谢你..

最佳答案

您说“检查 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);

关于mysql - 在mysql中查找记录时出现这种情况该如何处理?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6800228/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com