我需要创建一个男人的数据库,男人可以有一个或多个属性,每个男人的属性都有一个特定的值,听起来很简单吧?好吧,继续阅读,因为这个问题有点不可能(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)
);
使用此示例数据:
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);
并创建此查询:
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;
这给了我这个结果:
+------+-----------+-------+
| guy | attribute | value |
+------+-----------+-------+
| John | age | 12 |
| John | dollars | 15 |
| John | candies | 3 |
| Bob | age | 15 |
| Bob | dollars | 20 |
| Bob | candies | 6 |
+------+-----------+-------+
这是真正的问题:
后来,我的老板告诉我,他想使用尽可能多的条件来过滤数据,并且能够将这些条件与“and”和“or”分组,例如,他可能想做这个疯狂的条件:
获取年龄大于 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)
我创建过滤器没有问题(我使用 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)
)
但只有鲍勃被返回:(我应该得到约翰和鲍勃。
那么,我应该如何混合过滤器和查询?
请记住,每个人拥有的属性数量对于所有人来说都是相同的,但是可以随时添加更多属性和更多人,例如,如果我想添加“马里奥”这个人,我会这样做:
-- 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);
如果我想创建属性“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);
现在我应该能够在查询中包含有关苹果的条件。
我希望我的表述能让人理解,谢谢您的宝贵时间:)
注意:也许如果有一种方法可以将每个人的所有属性放在一行中?就像这样:
+------+-----------+-------+------+------------+--------+------+------------+--------+------+------------+--------+
| 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 |
+------+-----------+-------+------+------------+--------+------+------------+--------+------+------------+--------+
注2:@iim 建议(在这个问题中:如何在MySQL中的分组列中进行搜索? (如果可能的话,也可以在休眠状态下) https://stackoverflow.com/questions/7801644/how-to-search-in-grouped-columns-in-mysql-also-in-hibernate-if-possible)我可以为每个属性进行自连接,是的,这可能会解决问题,但是当人们拥有大量属性(例如 30 个或更多)时,可能会出现性能问题。
注 3:我无法更改数据库架构:(