3

我有一个看起来像这样的表(除了更多条目),我需要从所有 taxstatuscode = Paid 和 taxpaidtoname = NO 的条目中获取boatuid。但我无法让它工作。

桌子:

boatuid    slug              fieldValue
20         taxstatuscode     Paid
20         taxpaidtoname     NO
24         taxstatuscode     Paid
24         taxpaidtoname     SE
25         taxstatuscode     Not Paid
25         taxpaidtoname     N/A

询问:

SELECT a.boatuid
FROM tx_gcnwnxmlparser_boats_specs
LEFT JOIN tx_gcnwnxmlparser_boats_specs a ON (a.slug = "taxstatuscode")
LEFT JOIN tx_gcnwnxmlparser_boats_specs b ON (b.slug = "taxpaidtoname")
WHERE a.boatuid IN(20,24,25)
AND a.fieldValue = "Paid" 
AND b.fieldValue = "NO"
GROUP BY a.boatuid

现在它会返回 IN() 中的所有boatuid,而它应该只返回 20。我对 EAV 很陌生并且加入了,所以我做错了什么?

4

2 回答 2

1

使用 EAV 时的关键之一是使用“实体 ID”进行连接,以便您选择的属性属于同一实体。

SELECT a.boatuid
    FROM tx_gcnwnxmlparser_boats_specs a
        INNER JOIN tx_gcnwnxmlparser_boats_specs b 
            ON a.boatuid = b.boatuid /* "Entity ID" join condition */
                AND b.slug = 'taxpaidtoname'
    WHERE a.boatuid IN (20,24,25)
        AND a.slug = 'taxstatuscode'
        AND a.fieldValue = 'Paid'
        AND b.fieldValue = 'NO';
于 2012-08-29T13:34:12.413 回答
0

左连接返回左表中的所有行,无论条件如何认为您正在寻找内连接“TABLEA,TABLEB”或“TABLEA INNER JOIN TABLEB”:

TABLE_A LJ TABLE_B

返回:

TABLE_A row 1 | TABLE_B row  (which meets condtion)
TABLE_A row 1 | TABLE_B row  (which meets condtion)
TABLE_A row 1 | TABLE_B row  (which meets condtion)
TABLE_A row 2 | TABLE_B row  (which meets condtion)
TABLE_A row 2 | TABLE_B row  (which meets condtion) 
TABLE_A row 2 | TABLE_B row  (which meets condtion)
.
.
.
TABLE_A last row | TABLE_B row  (which meets condtion)
于 2012-08-29T13:27:56.100 回答