我看似是一组有效的SQL语句,它们在下拉方案中运行。第一个是全包式的AND,其中每个请求的选项都匹配。如果不这样做,它将转到第二个基于OR的sql调用。
有人可以向我解释或提供更好的陈述,其中第二层OR场景首先按大多数相关排序,这意味着大多数选项都匹配为第一个条目并从那里开始下降?
或者,或者,如果您有更好的方法来处理此搜索,可能只需一次调用,那就更好了。
第一个基于AND的SQL调用:
#include <iostream>
#include <vector>
class A {
public:
A(int);
private:
int value1;
};
class B {
public:
B(int);
private:
int value1;
};
B::B(int x) : value1(x) { std::cout << "ctor B(x)" << std::endl; }
A::A(int x) : value1(x) { std::cout << "ctor A(x) x=" << x <<std::endl; }
int A(int x) {
std::cout << "inside A(int x) x=" << x << std::endl;
x=x+1;
return x;
}
int main(){
class A a(0);
a = A(7);
B b(0);
return 0;
}
基于OR的第二层调用:
SELECT
l.id,l.title,l.city
FROM listings l
JOIN listings_facilities_xref x ON l.id = x.listing_id
WHERE l.property_type = '1'
AND x.facility_id = '1'
AND x.facility_id = '8'
AND x.facility_id = '54'
AND x.facility_id = '11'
AND l.city = 'Orlando'
AND l.state = '16'
GROUP BY l.id
我的表格布局如下:
SELECT l.id,l.title,l.city
FROM listings l
JOIN listings_facilities_xref x ON l.id = x.listing_id
WHERE l.property_type = '1'
AND (
x.facility_id = '1'
OR x.facility_id = '8'
OR x.facility_id = '54'
OR x.facility_id = '11'
)
AND l.city = 'Orlando'
AND l.state = '16'
GROUP BY l.id
示例导入数据可在以下位置找到:
商品目录:https://pastebin.com/EeuaEFrR
listings_facilities_xref:https://pastebin.com/7WeHgEaE
答案 0 :(得分:1)
我怀疑你想要:
SELECT l.id, l.title, l.city
FROM listings l
JOIN listings_facilities_xref x ON l.id = x.listing_id
WHERE l.property_type = 1 AND
x.facility_id IN (1, 8, 54, 11) AND
l.city = 'Orlando' AND
l.state = 16
GROUP BY l.id, l.title, l.city
ORDER BY COUNT(*) DESC;