我的表格布局的简化版本如下:
表项
+----+-------+-----+
| ID | sdesc | ... |
+----+-------+-----+
|  1 | item1 | ... |
+----+-------+-----+
表 itemaffectTable (调用这些 qProps 以获得具有数量的属性)
+--------+---------+----------+
| itemID | affectID| quantity |
+--------+---------+----------+
|      1 |       2 |       10 | // item 1 has affect 2 for a value of 10
|      1 |       3 |        2 | // item 1 has affect 3 for a value of 2
|      2 |       1 |        5 | // item 2 gets aff 1 for 5
|      2 |       1 |        6 | // item 2 gets aff 1 for 6 which means 11 total
|      3 |       5 |        5 | 
+--------+---------+----------+
表 itemaffectbyTable (称这些 bProps 存在时它们是相关的)
+--------+---------+
| itemID | affbyID |
+--------+---------+
|      1 |       6 |
|      3 |       2 |
|      3 |       3 |
+--------+---------+
样本输出:
itemID  sdesc           qpropID value   bpropID
1221    a copper lantern      4     2   5
1221    a copper lantern     18     2   5
1221    a copper lantern     17    -5   5
 477    a shade              19     3   4
 477    a shade              19     3   6
这在两个方面是不正确的。对于第一项,affectbyID 5 重复 3 次……这是可以容忍的。在第二种情况下,我们将影响 ID 19 和影响值 3 重复两次,这是不允许的。
理想情况下我想看看
itemID  sdesc           qpropID value   bpropID
1221    a copper lantern      4     2   5
1221    a copper lantern     18     2   NULL
1221    a copper lantern     17    -5   NULL
 477    a shade              19     3   4
 477    a shade            NULL  NULL   6
主要问题是 qpropID 和值的重复,因为它们是相加的。如果解决方案重复 bpropID,那没什么大不了的。
** 更新 **
我试图使用 FULL JOIN 的想法来获得我的结果,但似乎无法归零。
我最接近我想要的结果来自使用 sqlFiddle 来获得
select i.id, i.sdesc, iaft.affectID, iaft.amount, NULL FROM item i
    LEFT JOIN itemaffectTable iaft ON i.id=iaft.itemID
UNION
select i.id, i.sdesc, NULL, NULL, iafbt.affectbyID FROM item i
    LEFT JOIN itemaffectedbyTable iafbt ON i.id=iafbt.itemID
ORDER BY id
所以最重要的想法是我想检索符合过滤器标准的项目列表,然后将这些项目与其关联的 effectID 和 effectbyID 匹配。
原文如下。
SELECT DISTINCT i.id, i.sdesc, iaft.affectID, iaft.amount, iafbt.affectbyID FROM item i
INNER JOIN itemwearTable iwt ON i.id=iwt.itemID 
    LEFT JOIN itemaffectTable iaft ON i.id=iaft.itemID
    LEFT JOIN itemaffectedbyTable iafbt ON i.id=iafbt.itemID
LEFT JOIN itemalignTable iat ON i.id = iat.itemID
LEFT JOIN itemgenderTable igt ON i.id = igt.itemID
LEFT JOIN itemgenreTable igrt ON i.id = igrt.itemID
LEFT JOIN itemclassTable ict ON i.id = ict.itemID
LEFT JOIN itemraceTable irt ON i.id = irt.itemID
WHERE (iat.itemID IS NULL OR iat.alignID = 1)
AND (igt.itemID IS NULL OR igt.genderID = 1)
AND (igrt.itemID IS NULL OR igrt.genreID = 1)
AND (ict.itemID IS NULL OR ict.classID = 1)
AND (irt.itemID IS NULL OR irt.raceID = 1)
AND i.minlvl <= 50
AND iwt.wearlocID=1
ORDER BY sdesc