[英]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: 您可以使用LIKE和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%'
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
的子字符串逻辑不能保证完整性之外,它不是很方便,因为很难甚至不能在约束中使用它。查询效率较低,因为需要像LIKE
或substring()
这样的字符串操作。同时具有共享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.