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.
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.
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.
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.
You can use the LIKE and NOT LIKE clauses to filter by sequence of characters:
For example:
1. Select all people where names begin with the letter 'c':
SELECT * FROM person WHERE name LIKE 'c%'
2. Select all people where second letter of the name is 'c':
SELECT * FROM person WHERE name LIKE '_c%'
3. Select all persons who have the sequence 'clau' in the name independent to their position:
SELECT * FROM person WHERE name LIKE '%clau%'
4. Select all people where name does not start with the letter '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:
SELECT * FROM person WHERE name NOT LIKE '%applefritter_demo_rasberry%'
For your problem the solution would be to use LIKE as follows:
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? (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?)
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:
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. 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.)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.