SELECT DISTINCT business.name AS businessname
,business.description AS description
FROM business
, category
, sub_categories
WHERE business.cityID = '$city'
AND (category.name LIKE '%$name%'
OR sub_categories.name LIKE '%$name%')
AND business.status = 0
请假设上面的SQL代码在括号中搜索一组两个表并返回结果,但由于某种原因,它没有这样做。我做错了什么?
谢谢。
答案 0 :(得分:2)
您的查询会产生cartesian product。根据表格的大小,可能需要相当长的时间。
根据您的说明,我会使用子查询来检查匹配的类别,这样您就不必在查询中使用distinct,因为它只会返回每个业务一次。我还建议你从一个不错的SQL教程开始。
SELECT name AS businessname
,description AS description
FROM business
WHERE cityID = '$city'
AND status = 0
AND ( categoryID in (select id from category where name like '%$name%')
or subcategoryID in (select id from sub_categories where name like '%$name%')
)
答案 1 :(得分:0)
我想到了两件事: