简体   繁体   English

MySQL查询返回的行比预期的多,我如何仅从每个组返回一列的最大值?

[英]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.

相关问题 如何根据表格列中的最大值从Mysql返回行? - How do you return rows from Mysql based on highest value in table column? 如果结果不止一个,则对db中一个字段的值进行排名,并仅返回最高/最低排名值 - If more than one result, rank values from one field in db and return only the highest/lowest ranked value SQL查询仅返回特定列中具有最高值的行 - SQL query to only return the rows that have the highest value in a certain column 如何只从表中选择行一个clumns值超过MySQL中的b表计数值 - How to select only rows from a table one clumns value is more than b table count value in MySQL mysql查询仅在行数大于零时返回 - mysql query return only if number of rows is more than zero 当一个mysql值有多个值时,如何从一个mysql查询中消除一个特定值,而当只有一个值时,如何保持原样 - How to eliminate a particular value from a mysql query when it has more than one value and leave as it is when it has only one value MySQL查询返回多个值-应该返回一个 - mysql query returning more than one value - should return one 优化查询-如何从日期时间列中选择最高和最低行,按日期分组并返回数组 - Optimize query - How can I Select highest and lowest row, group by date from datetime column and return array 如何按每个组的最高值对组进行排序 - How do I order groups by each group's highest value 如何将某个列属性的所有行分组,然后从最高到最低排序? - How do I group all rows of a certain column attribute and then order them from highest to lowest?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM