我还需要从xcart_extra_field_values中选择值WHERE fieldid = 5.从环顾四周来看,我需要做的是使用连接和别名,但是我似乎无法弄清楚如何加入表格本身,只是使用另一个别名的值,并试图拉入同一个表两次崩溃我的数据库。两次。
这是我当前的查询,有效:
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)
这是崩溃数据库的查询:
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)
答案 0 :(得分:2)
这是我写的方式:
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
假设您已准备好所有其他索引,那么您的xpe5
值检查可能会让您失望。
我永远不会让员工从文本框中将固定的股票位置输入数据库..您应该至少有一组值的下拉列表(可能让员工为新的股票位置添加值)。我可能会将库存位置选项放在一个单独的表中,并在主表中放入一个外键。
答案 1 :(得分:0)
我现在已经开始工作了。典型的时候我发布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)
我还需要将c.productid与d.productid进行比较。但是,我对自己的答案并不满意,因为它看起来非常低效。不幸的是,这是由于我对mysql的理解有限。