MySQL EAV查询返回所有条目

时间:2012-08-29 12:17:58

标签: php mysql entity-attribute-value

我有一个看起来像这样的表(除了更多的条目),我需要从taxstatuscode =付费和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()中所有的船只,当它应该返回20时。 我对EAV很新,加入,所以我做错了什么?

2 个答案:

答案 0 :(得分: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';

答案 1 :(得分: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)