简体   繁体   English

两次选择同一列

[英]Selecting same column twice

I need to also select value from xcart_extra_field_values WHERE fieldid = 5. From looking around, it seems what I need to do is use a join and aliases, however I can't seem to figure out how to join the table on itself, and just using another alias for value, and trying to pull in the same table twice crashed my database. 我还需要从xcart_extra_field_values WHERE fieldid = 5中选择值。从环顾四周,似乎我需要做的是使用联接和别名,但是我似乎无法弄清楚如何将表本身联接起来,只是使用另一个别名作为值,并尝试两次在同一张表中提取数据,导致数据库崩溃。 Twice. 两次。

This my current query, which works: 这是我当前的查询,它的工作原理是:

                            SELECT a.avail,
                                c.value,
                                b.productid,
                                a.productcode,
                         FROM xcart_products a,
                              xcart_products_lng_en b,
                              xcart_extra_field_values c,
                         WHERE a.productid IN
                             (SELECT productid
                              FROM xcart_extra_field_values
                              WHERE a.productid = productid
                                AND fieldid = 5
                                AND (LOWER(value) = 'retail'
                                     OR LOWER(value) = 'stock'
                                     OR LOWER(value) = 'c1 stock'
                                     OR LOWER(value) = 'c2 stock'
                                     OR LOWER(value) = 'g stock') )
                           AND c.fieldid = 9
                           AND (a.productid = b.productid)
                           AND (b.productid = c.productid)

This is the query that crashes the database: 这是使数据库崩溃的查询:

                            SELECT a.avail,
                                c.value,
                                d.value,
                                b.productid,
                                a.productcode
                         FROM xcart_products a,
                              xcart_products_lng_en b,
                              xcart_extra_field_values c,
                              xcart_extra_field_values d
                         WHERE a.productid IN
                             (SELECT productid
                              FROM xcart_extra_field_values
                              WHERE a.productid = productid
                                AND fieldid = 5
                                AND (LOWER(value) = 'retail'
                                     OR LOWER(value) = 'stock'
                                     OR LOWER(value) = 'c1 stock'
                                     OR LOWER(value) = 'c2 stock'
                                     OR LOWER(value) = 'g stock') )
                           AND c.fieldid = 9
                           AND d.fieldid = 5
                           AND (a.productid = b.productid)
                           AND (b.productid = c.productid)

This is how I'd write it: 这是我的写法:

 SELECT xp.avail,
        xpe5.value,
        xpe9.value,
        xpl.productid,
        xp.productcode
   FROM xcart_products xp
   JOIN xcart_products_lng_en xpl
     ON xpl.productid = xp.productid
   JOIN xcart_extra_field_values xpe5
     ON xpe5.productid = xp.productid
    AND xpe5.fieldid = 5
    AND xpe5.value IN ('retail','stock','c1 stock','c2 stock','g stock')
   JOIN xcart_extra_field_values xpe9
     ON xpe9.productid = xp.productid
    AND xpe9.fieldid = 9

Assuming you have all other indexes in place, your xpe5 value check is probably what's letting the side down. 假设您拥有所有其他索引,则xpe5值检查很可能会让您失望。

I would never let the employee input a fixed stock location into the database from a textbox.. you should have a dropdown with a set of values at the very least (maybe let the employee add values for new stock locations). 我绝不会允许员工从文本框向数据库中输入固定的库存地点。您应该至少有一个包含一组值的下拉列表(也许让员工为新的库存地点添加值)。 I'd probably put the stock location options in a separate table and put in a foreign key to that in the main table. 我可能将库存位置选项放在单独的表中,并在主表中放入外键。

I've got it working now. 我现在可以正常工作了。 Typical when I post mysql questions. 当我发布mysql问题时很典型。

                            SELECT a.avail,
                                    c.value,
                                    d.value,
                                    b.productid,
                                    a.productcode
                             FROM xcart_products a,
                                  xcart_products_lng_en b,
                                  xcart_extra_field_values c,
                                  xcart_extra_field_values d
                             WHERE a.productid IN
                                 (SELECT productid
                                  FROM xcart_extra_field_values
                                  WHERE a.productid = productid
                                    AND fieldid = 5
                                    AND (LOWER(value) = 'retail'
                                         OR LOWER(value) = 'stock'
                                         OR LOWER(value) = 'c1 stock'
                                         OR LOWER(value) = 'c2 stock'
                                         OR LOWER(value) = 'g stock') )
                               AND c.fieldid = 9
                               AND d.fieldid = 5
                               AND (a.productid = b.productid)
                               AND (b.productid = c.productid)
                               AND (c.productid = d.productid)

I need to also compare c.productid to d.productid. 我还需要比较c.productid和d.productid。 However, I'm not pleased with my own answer, as it seems terribly inefficient. 但是,我对自己的回答不满意,因为它似乎效率很低。 This is, unfortunately, due to my own limited understanding of mysql. 不幸的是,这是由于我对mysql的了解有限。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM