我需要创建一个男人的数据库,男人可以有一个或多个属性,每个人的属性都有一个特定的值,听起来很容易吗?好吧,继续阅读,因为问题有点不可能(5天处理它:s).
所以我创建了这3个表:
CREATE TABLE guy (
id int(11),
name varchar(255)
);
CREATE TABLE attribute (
id int(11),
name varchar(255)
);
-- each value references one guy and one attribute
CREATE TABLE _value (
id int(11),
guy_id int(11),
attribute_id int(11),
_value varchar(255)
);
Run Code Online (Sandbox Code Playgroud)
使用此示例数据:
INSERT INTO attribute VALUES (1, 'age'), (2, 'dollars'), (3, 'candies');
INSERT INTO guy VALUES (1, 'John'), (2, 'Bob');
INSERT INTO _value VALUES (1, 1, 1, 12), (2, 1, 2, 15), (3, 1, 3, 3);
INSERT INTO _value VALUES (4, 2, 1, 15), (5, 2, 2, 20), (6, 2, 3, 6);
Run Code Online (Sandbox Code Playgroud)
并创建此查询:
SELECT g.name 'guy', a.name 'attribute', v._value 'value'
FROM guy g
JOIN _value v ON g.id = v.guy_id
JOIN attribute a ON a.id = v.attribute_id;
Run Code Online (Sandbox Code Playgroud)
这给了我这个结果:
+------+-----------+-------+
| guy | attribute | value |
+------+-----------+-------+
| John | age | 12 |
| John | dollars | 15 |
| John | candies | 3 |
| Bob | age | 15 |
| Bob | dollars | 20 |
| Bob | candies | 6 |
+------+-----------+-------+
Run Code Online (Sandbox Code Playgroud)
这是真正的问题:
后来,我的老板告诉我他希望使用尽可能多的条件来过滤数据,因为他希望能够用"ands"和"ors"对这些条件进行分组,例如,他可能想要做这种疯狂的情况:
获取家伙哪个年龄大于10,小于18美元,有超过2种糖果和小于10分的糖果,但无论怎样,还包括球员哪个年龄正好是15.这将转化为这个过滤器:
-- should return both John and Bob
(age > 10 and dollars < 18 and candies > 2 and candies < 10) or (age = 15)
Run Code Online (Sandbox Code Playgroud)
我没有创建过滤器的问题(我使用jqgrid),问题是属性不是列,而是行而不是因为我不知道如何将查询与过滤器混合,我尝试了这样的事情:
SELECT g.name 'guy', a.name 'attribute', v._value 'value'
FROM guy g
JOIN _value v ON g.id = v.guy_id
JOIN attribute a ON a.id = v.attribute_id
GROUP BY guy
HAVING (
(attribute = 'age' and value > 10) AND
(attribute = 'dollars' and value < 18) AND
(attribute = 'candies' and value > 2) AND
(attribute = 'candies' and value < 10)
)
OR
(
(attribute = 'age' and value = 15)
)
Run Code Online (Sandbox Code Playgroud)
但只有鲍勃回来了:(我应该得到约翰和鲍勃.
那么,我应该如何混合过滤器和查询?
有记住,每个属性都有人的数量是所有男人一样的,但更多的属性和更多的人可以随时添加,例如,如果我想补充的家伙"马里奥"我会做:
-- we insert the guy Mario
INSERT INTO guy VALUES (3, 'Mario');
-- with age = 5, dollars = 100 and candies = 1
INSERT INTO _value VALUES (7, 3, 1, 5), (8, 3, 2, 100), (9, 3, 3, 1);
Run Code Online (Sandbox Code Playgroud)
如果我想创建属性'apples',我会这样做:
-- we insert the attribute apples
INSERT INTO attribute VALUES (4, 'apples');
-- we create a value for each guy's new attribute, John as 7 apples, Bob has 3 and Mario has 8
INSERT INTO _value VALUES (10, 1, 4, 7), (11, 2, 4, 2), (12, 3, 4, 8);
Run Code Online (Sandbox Code Playgroud)
现在我应该能够在我的查询中包含有关苹果的条件.
我希望我让自己理解,谢谢你所有的时间:)
注意:也许如果有办法将每个人的所有属性放在一行中?,这样的事情:
+------+-----------+-------+------+------------+--------+------+------------+--------+------+------------+--------+
| guy | attribute | value | guy | attribute | value | guy | attribute | value | guy | attribute | value |
+------+-----------+-------+------+------------+--------+------+------------+--------+------+------------+--------+
| John | age | 12 | John | dollars | 15 | John | candies | 3 | John | apples | 7 |
| Bob | age | 15 | Bob | dollars | 20 | Bob | candies | 6 | Bob | apples | 2 |
| Mario| age | 5 | Mario| dollars | 100| Mario| candies | 1 | Mario| apples | 8 |
+------+-----------+-------+------+------------+--------+------+------------+--------+------+------------+--------+
Run Code Online (Sandbox Code Playgroud)
注2:@iim建议(在这个问题:如何在MySQL中的分组列中搜索?(如果可能的话,也在Hibernate中))我可以为每个属性进行自联接,是的,这可以解决问题,但是当人们拥有大量属性(如30或更多)时,可能会出现性能问题.
注3:我无法更改数据库架构:(
下面的内容可以让你或多或少地简单化你的条件,尽管我不能保证它对于 100,000 多个具有 30 多个属性的人来说会真正有效。你应该亲自看看。
SELECT g.name guy, a.name attribute, v._value value
FROM guy g
JOIN _value v ON g.id = v.guy_id
JOIN attribute a ON a.id = v.attribute_id
GROUP BY guy
HAVING (
SUM(a.name = 'age' and v._value > 10) = 1 AND
SUM(a.name = 'dollars' and v._value < 18) = 1 AND
SUM(a.name = 'candies' and v._value > 2 ) = 1 AND
SUM(a.name = 'candies' and v._value < 10) = 1
)
OR
(
SUM(a.name = 'age' and v._value = 15) = 1
)
Run Code Online (Sandbox Code Playgroud)
(我在这里假设一个人不能有重复的属性。)