简体   繁体   English

忽略子字符串匹配时具有最高值的列

[英]Ignoring column with the highest value where substring matches

I have a table similar to this in my application, i am trying to ignore the item "applefritter_demo_rasberry" because its just the sum of all the other items which has the same substring. 我的应用程序中有一个与此表类似的表,我试图忽略项目“ applefritter_demo_rasberry”,因为它只是具有相同子字符串的所有其他项目的总和。

I am trying to write a mysql query (not an expert) to fetch all the values except "applefritter_demo_rasberry" which is nothing but a parent to these items and holds the total. 我正在尝试编写一个mysql查询(不是专家)来获取除“ applefritter_demo_rasberry”之外的所有值,这些值不过是这些项目的父项并保存总数。

I've looked at the sub string using this post , but i couldn't grasp the logic behind this. 我已经看过这篇文章中的子字符串,但是我无法理解其背后的逻辑。 Can some one suggest a way. 有人可以提出一种建议。

ID        Recipe                                 Value 
164686    applefritter_demo_rasberry             140                      
164686    applefritter_demo_rasberry_cake        40
164686    applefritter_demo_rasberry_pudding     20
164686    applefritter_demo_rasberry_pie         10
164686    applefritter_demo_rasberry_bread       40
164686    applefritter_demo_rasberry_candy       20
164686    applefritter_demo_rasberry_juice       10

EDIT: 编辑:

Just to add a bit more clarity to the question, the table is populated by an application, the application also inserted the parent recipe's total and its children's total. 只是为了使问题更清楚一点,该表由应用程序填充,该应用程序还插入了父配方的总和及其子代的总和。 I have a requirement to identify the total of all the recipe's in the table, the table also contains standalone recipe (no parent) such as "ovenfresh_frenchbread" , during total calculation i want ignore all these parent items who end up being the substring of child items. 我需要识别表中所有配方的总数,该表还包含独立配方(无父项),例如“ ovenfresh_frenchbread” ,在总计计算期间,我想忽略所有最终成为子项子项的父项项目。

As I understand the question, you want something like this: 据我了解的问题,您想要这样的事情:

select t.*
from t
where t.value <> (select coalesce(sum(t2.value), 0)
                  from t2
                  where t2.recipe like concat(t.recipe, '_%') and
                        t2.id = t.id
                 );

I am not 100% clear on what the matching logic is in the subquery, whether it is based only on id or also on string matching. 我不是100%清楚子查询中的匹配逻辑是什么,无论它仅基于id还是基于字符串匹配。

You can use the LIKE and NOT LIKE clauses to filter by sequence of characters: 您可以使用LIKENOT LIKE子句按字符序列进行过滤:

  1. The LIKE clause determines if the string matches a specified pattern. LIKE子句确定字符串是否与指定的模式匹配。 A pattern can include normal characters and wildcards. 模式可以包含普通字符和通配符。
  2. NOT LIKE reverses the comparison, verifying that the string does NOT match the specified pattern. NOT LIKE反转比较,并验证字符串与指定的模式不匹配。

For example: 例如:

1. Select all people where names begin with the letter 'c': 1.选择姓名以字母“ c”开头的所有人员:

SELECT * FROM person WHERE name LIKE 'c%'

2. Select all people where second letter of the name is 'c': 2.选择姓名第二个字母为“ c”的所有人员:

SELECT * FROM person WHERE name LIKE '_c%'

3. Select all persons who have the sequence 'clau' in the name independent to their position: 3.选择名称独立于其位置的所有名称为“ clau”的人:

SELECT * FROM person WHERE name LIKE '%clau%'

4. Select all people where name does not start with the letter 'c': 4.选择姓名不以字母“ c”开头的所有人员:

SELECT * FROM person WHERE name NOT LIKE 'c%'

Your Solution 您的解决方案

Going back to your problem, if we use NOT LIKE as below, no results will be returned, because all the items in your column start with this sequence: 回到您的问题,如果我们按以下方式使用NOT LIKE,则不会返回任何结果,因为您列中的所有项目均以以下顺序开头:

SELECT * FROM person WHERE name NOT LIKE '%applefritter_demo_rasberry%'

For your problem the solution would be to use LIKE as follows: 对于您的问题,解决方案是使用LIKE,如下所示:

SELECT * FROM person WHERE name LIKE '%applefritter_demo_rasberry_%'

Hope this helps! 希望这可以帮助!

Consider adding another column Parent holding the ID of the parent, as with the shared IDs it isn't clear which row has which role -- is it a parent or a child? 考虑添加另一列“ Parent ”,其中包含父级ID,因为与共享ID并不清楚哪一行具有哪个角色-是父级还是子级? (That is, apart from the substring logic in Recipe which isn't that handy to guarantee integrity as it's hard if not impossible to use in constraints. And is less efficient to query as string operations like LIKE or substring() needed to be involved. Plus it's redundant having the shared ID logic and the substring logic in place at the same time. What's right if they contradict each other?) (也就是说,除了Recipe的子字符串逻辑不能保证完整性之外,它不是很方便,因为很难甚至不能在约束中使用它。查询效率较低,因为需要像LIKEsubstring()这样的字符串操作。同时具有共享ID逻辑和子字符串逻辑是多余的。如果它们相互矛盾,那又如何呢?)

Redesigned like that it's easy to query the direct children of a parent. 像这样重新设计,可以很容易地查询父母的直子。 Just select all rows where the Parent is the one you're looking for, eg: 只需选择所有要查找“ Parent行,例如:

SELECT *
       FROM table
       WHERE Parent = 164686;

And think about filling the Value of parents automatically to the sum of their children eg by triggers, unless you've done that already. 并考虑将父母的Value自动计入其子女的总和,例如通过触发器,除非您已经这样做。 Otherwise that might get inconsistent. 否则可能会导致不一致。 Or alternatively only store the value in row without any children and compute the value of a parent by the sum of the values of its children. 或者,仅将值存储在行中而不包含任何子项,并通过其子项值的总和来计算父项的值。 (Though this can get hairy if there is more than one level of hierarchy involved.) (但是,如果涉及的层次结构不止一个级别,那么这可能会很麻烦。)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 选择另一个列具有最高值的值 - Select a value where another column has the highest value 如何从表中第3列匹配值而第4列匹配值的表的第4列中获取值? - How to get the value from column 4 in a table where column 3 matches a value and column 4 matches a value ? MySQL:当 WHERE 找到相似条目时选择最高列值 - MySQL: SELECT highest column value when WHERE finds similar entries 选择特定列具有最高值的行 - Select row where specific column has the highest value "Mysql:查找列值以特定子字符串结尾的行" - Mysql: Find rows where column value ends with a specific substring B列表2中的返回值,其中A列表1匹配A列表2 - Return Value in Column B Table 2, where column A Table 1, matches Column A Table 2 从单独列中的值匹配 3 个不同值的列中查找共同值 - Find common value from a column where value from separate column matches 3 different values 如何查询只有特定列中具有最高值的行出现的行? - How to query rows where only the rows with the highest value in a specific column appear? 识别SQL中匹配次数最多的列中的ID对 - Identifying the pairs of ID's in a column with the highest number of matches in SQL 将值从一个表中的列复制到另一个表中的列,其中另一个值与MySQL匹配 - Copy the values from a column in one table to a column in another table where another value matches MySQL
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM