[英]MySQL query returns more rows than intended, how do i only return the highest value of one column from each group?
This query needs to return all rows with the highest value of 'revision' column out of each group of 'sheet_ID' column. 该查询需要返回“ sheet_ID”列的每组中具有“ revision”列最高值的所有行。 It works fine when i do not do the OR statement but i need to be able to use them.
当我不执行OR语句但我需要能够使用它们时,它会很好地工作。 The input field is for 'Item Numbers' and the or statement needs to pull that number from a possible of 10 columns where it can be.
输入字段用于“项目编号”,or语句需要从可能的10列中拉出该编号。
Here is my Query statement: 这是我的查询语句:
SELECT item_no, item_no1, item_no2, item_no3, item_no4, item_no5, item_no6,
item_no7, item_no8, item_no9, style, vendor_code, pgc, buyer, brand,
product_name, sheet_ID, revision, id
FROM product_sheets
WHERE item_no LIKE '$value' or item_no1 LIKE '$value' or
item_no2 LIKE '$value' or item_no3 LIKE '$value' or
item_no4 LIKE '$value' or item_no5 LIKE '$value' or
item_no6 LIKE '$value' or item_no7 LIKE '$value' or
item_no8 LIKE '$value' or item_no9 LIKE '$value' AND
revision IN (SELECT MAX(revision)
FROM product_sheets
GROUP BY sheet_ID
);
Instead of this: 代替这个:
revision IN (SELECT MAX(revision)
FROM product_sheets
GROUP BY sheet_ID
);
You want a correlated subquery. 您需要一个相关的子查询。 In your case, you can write it as:
就您而言,您可以将其编写为:
revision = (SELECT MAX(ps2.revision)
FROM product_sheets ps2
WHERE ps2.sheet_ID = product_sheets.sheet_ID
);
However, I would strongly advise you to use a table alias in the outer query. 但是,我强烈建议您在外部查询中使用表别名。
You also need to fix the parentheses in the where
: 您还需要在以下
where
修复括号:
WHERE (item_no LIKE '$value' or item_no1 LIKE '$value' or
item_no2 LIKE '$value' or item_no3 LIKE '$value' or
item_no4 LIKE '$value' or item_no5 LIKE '$value' or
item_no6 LIKE '$value' or item_no7 LIKE '$value' or
item_no8 LIKE '$value' or item_no9 LIKE '$value'
) AND
revision = (SELECT MAX(ps2.revision)
FROM product_sheets ps2
WHERE ps2.sheet_ID = product_sheets.sheet_ID
);
If $value
doesn't use wildcards, this is much more simply written as: 如果
$value
不使用通配符,则更简单地写为:
where '$value' in (item_no, item_no1, item_no2, . . .) and
revision = (SELECT MAX(ps2.revision)
FROM product_sheets ps2
WHERE ps2.sheet_ID = product_sheets.sheet_ID
);
And, in addition to the comment about normalization (which is strongly suggested), you also need to learn about using query parameters to pass in values to a query. 并且,除了有关规范化的注释(强烈建议)外,您还需要学习有关使用查询参数将值传递给查询的知识。
You must use brackets around th or
conditions, without them only the last condition item_no9 LIKE '$value'
is related with an AND operator to the IN
condition. 因此必须将日方括号
or
条件,没有他们只有最后一个条件item_no9 LIKE '$value'
与一个与运营商相关的IN
条件。
WHERE (item_no LIKE '$value' or item_no1 LIKE '$value' or
item_no2 LIKE '$value' or item_no3 LIKE '$value' or
item_no4 LIKE '$value' or item_no5 LIKE '$value' or
item_no6 LIKE '$value' or item_no7 LIKE '$value' or
item_no8 LIKE '$value' or item_no9 LIKE '$value'
)
In addition change the IN
condition to the following. 另外,将
IN
条件更改为以下内容。
You don't want a revision that is not the max revision of of its sheet_ID to match with a max revision of another sheet_ID. 您不希望不是与其sheet_ID的最大修订版匹配的修订版与另一个sheet_ID的最大修订版匹配。
AND (sheet_ID,revision) IN (SELECT sheet_ID,MAX(revision)
FROM product_sheets
GROUP BY sheet_ID
);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.