MYSQLI从列中选择字段并从同一个表

时间:2016-05-14 00:58:38

标签: php mysql select mysqli

在我的查询中,我试图让value attribute_id =' 142'仅当attribute_id 141 =' K& N'

table

如果attribute_id 141 =其他任何内容,例如Flowmaster,那么我不想要attribute_id 142行

不确定我将如何编写此查询。

到目前为止,我正在尝试下面的联接,但它不起作用。

$query = "
SELECT
part.value as part,
brand.value as brand

FROM catalog_product_entity_varchar part

LEFT JOIN catalog_product_entity_varchar brand
ON part.value = 'K&N' AND part.attribute_id = '141'

WHERE part.attribute_id = '142'

LIMIT 0, 2";

$result = $mysqli->query($query);

if (!$result) {
    die('invalid query');
}

while($row = $result->fetch_array(MYSQL_ASSOC)) {
    $part = $row['part'];
}

更新

我想我现在明白了。我把它改成了

$query = "
SELECT
part.value as part,
brand.value as brand

FROM catalog_product_entity_varchar part

JOIN catalog_product_entity_varchar brand
ON part.entity_id = brand.entity_id AND brand.attribute_id = '141' AND brand.value = 'K&N'

WHERE part.attribute_id = '142'

LIMIT 0, 2";

$result = $mysqli->query($query);

if (!$result) {
    die('invalid query');
}

while($row = $result->fetch_array(MYSQL_ASSOC)) {
    $part = $row['part'];
}

0 个答案:

没有答案