【问题标题】:Using "JOIN", "WHERE", and "IN" (mysql+php)使用 "JOIN"、"WHERE" 和 "IN" (mysql+php)
【发布时间】:2014-01-20 17:20:43
【问题描述】:
SELECT attribute_value.value 
FROM attribute_value 
INNER JOIN attributes ON attributes.id=attribute_value.attribute_id
WHERE attributes.attribute IN (colour, size);

我做的不对,但我得到的错误是 #1054 - 'where 子句'中的未知列 'Colour'

我正在尝试获取与之关联的属性的值。现在属性在“IN”子句中列出,但我想知道如何使用这样的关联数组:

Array([attributes] => Array ( [0] => Colour [1] => Size )

表属性值

______________________________
|  attribute_id  |  value    |
------------------------------
|  1             |  Red      |
|  1             |  Blue     |
|  2             |  Large    |

表格属性

____________________
|  id  | attribute |
--------------------
|  1   |  Colour   |
|  2   |  Size     |

为了澄清,将发布多个属性并仅使用这些属性,从属性值中获取值,其中属性中的属性 id 等于属性值中的属性 ID

【问题讨论】:

  • 试试这个attributes.attribute IN ('colour', 'size')
  • coloursize 确实是字符串常量。没有引号,mysql会将它们解释为字段名。

标签: php mysql sql


【解决方案1】:

您的 IN 有问题。您应该在单引号之间使用字符串。

SELECT attribute_value.value 
FROM attribute_value 
INNER JOIN attributes ON attributes.id=attribute_value.attribute_id
WHERE attributes.attribute IN ('Colour', 'Size');

【讨论】:

    【解决方案2】:

    在您的 WHERE attributes.attribute IN ('colour', 'size') 中试试这个。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-03-08
      • 1970-01-01
      • 2016-03-16
      • 2016-05-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多