get all children of a parent mysql one query for 5.6 - mysql-5.6

Table only has column
|id|Name|Parent
the parent being the root of the of the table
The result required
|Parent|child_id|Ancestral_Level
Child_ids All the succeeding Children where the parent
is their a way to do it on 5.6 as simple as possible?
Update
Now i just need the depth level to perent

I think you may find the answer you are looking for here: Hierarchical data in MySQL: parents and children in one query

Related

insert or update parent id to reference child records on same table

I have a PostgreSQL table in an application that holds both parent and child records. There is a column in the table to reference the the parent id where applicable for each child record. The problem is I am trying to import data from an external source where the child record is made up of a sub number of the parent. eg parent_reference_id = 123456000000 and a child_reference record for this could 123456000001, 123456000002 and so on. The application itself generates a unique id for each record when I import the data and so its possible to import the child and parent records simultaneously, however the difficulty I'm facing is linking the application generated id for the parent record to the parent_reference_id for the corresponding child records. The only hook I have is that the 1st six digits of the child_value_reference match the 1st six digits of the parent_value_reference and I've tried something like foo = bar(left(value,6)||'000000'; to create a match. However, I don't know how to use this to return the unique_id in a meaningful way and update the matching records. I've tried temporary tables and cte, however my knowledge of postgres is limited and I can't seem to find a solution that fits my problem. Another thing to mention is that these groups can change with updates within the external data so i'd also need a solution to make those updates too. Thanks in advance, Crispian

How to delete one record using frappe.db.delete syntax?

I’m looking to use frappe.db.delete to remove the most recently modified record in a custom table, Warehouse Locations. I want to limit the delete to only one record that matches some filters.
The table is a child table, if that matters.
I am not clear on how to filter one record, based on the “modified” value. I tried:
frappe.db.delete(‘Warehouse Locations’,
{“warehouse”: warehouse,
“parent”: item_code,
“shelf”: shelf,
“modified”:("=",last_record_to_keep[0].modified)})
I am getting a syntax error when I run the above query.
First, filter out the record to be deleted using ORM by running
record = frappe.get_list('Warehouse Locations', order_by='-modified')[0]
Once you filtered it out, you can delete it using frappe.db.delete.
frappe.db.delete('Warehouse Locations', record)
I think the solution answered by #ChillarAnand is helpful.
Instead, I would like to give a different way to solve the problem you faced.
Per your question, the goal is to delete only one record from Warehouse Locations (Child Table).
# 1. Get the parent document for the warehouse location.
parent_doc = frappe.get_doc("Doctype", docname)
# 2. iterate through the child table rows to find the row meet your filter
# and assign to row_to_detele for late use or you can delete straight away
row_to_delete = ""
for row in parent_doc.warehouse_locations:
if row.modified == last_record_to_keep[0].modified:
row_to_delete = row.name
break
# 3. to remove the child table from the parent doc method
parent_doc.remove(row_to_delete)
For the document of parent_doc.remove(), you can find it through the below github path: https://github.com/frappe/frappe/blob/6b91ade73c07dc1c070ed137cf54a29a3e7b0993/frappe/model/base_document.py#L210 (7 Oct, 2021)

Query and modify list of children with arbitrary positions

With the following schema in mind:
parent (
parent_id
child_id
)
child (
parent_id
child_id
position
)
I try to design a schema that allows me to retrieve a list of children, where children can be ordered arbitrarily. Adding a child to a parent is the same as appending it to the end of list, with its position equal to the maximum position of all positions for a given parent + 1. One should also be able to re-position a child with respect to its siblings, here, by changing the position columns of multiple child entries. One should also be able to delete a child, with their respective positions staying unchanged.
Querying the children of a given parent according to their position would follow this type of query:
select child.*
from child
where child.parent_id = $1
order by child.position asc;
I wonder whether this sort of schema would be adequate, as any manipulation, whether insertion of a child, or changing the position of a child, becomes quite complicated, as it involves querying/modifying existing records at first. Should we instead place the position data onto the parent table, as an array of some sort?
There are two common ways of modelling hierarchical date in relational database - the adjacency list and the nested set. Joe Celko has an entire book dedicated to this.
Your proposed model is based on the adjacency list. It's intuitive, but often hard to work with - retrieving a hierarchy requires recursive queries, and updates/inserts can be complicated.
The alternative is the nested set - which is much more efficient when querying, and inserts/updates are simpler (though do require more records to be modified).

Get Children of Parent Row

Is there any way to get all of the children of a parent row? The only method that I see, is to grab all of the rows and look at the parentId's assigned to the children.
(For what it is worth, I am using the javascript api)
I don't believe it's currently possible to explicitly request all child rows of a specified row, using the SmartSheet API.
As you've described in your post, you'd need to use the Get Sheet operation to get the list of all rows in the sheet, then look for row objects in that result set where parentId matches the id of the parent row you're interested in.

loading dataset where the dataadapter select command uses the IN clause with a subquery

I'm loading a subset of the parent records and the child records into a dataset then setting datarelations and foreignkeyconstraints, so when I am building the dataadapter select stmt for the child records, I must make sure that only the child records whose parent is present is loaded to avoid referential integrity errors. Since the subset of the ParentTable has been loaded into a dataset I tried:
daChild = new OleDBDataAdapter(CreateOledbCommand("select * from Childtable where ChildKey in (ds.tables(""ParentTable""))",dconn))
But got an "undefined function 'ds.tables' in expression" error at runtime.
So, I tried to create a one column table to pass to the IN clause, like this:
Dim MyDataView as DataView = New DataView(ds.tables("ParentTable"))
Dim MyTempParentTable as DataTable = MyDataView.ToTable(False, "ParentKey")
daChild = New OleDbDataAdapter(CreateOledbCommand("select * from ChildTable where ChildKey in (MyTempParentTable)", dconn))
I checked in the debugger, and MyTempParentTable is, in fact, a one column table containing the key of the ParentTable. I thought that the IN clause could take a one column datatable as valid input. Apparently not, as I am getting a "No value given for one or more required parameters" error at runtime.
I'm just about out of ideas. All my google searching came up with for the IN clause was something like IN (value1, value2, value3...) and doesn't give any examples for the IN clause containing a more complex expression.
Can anyone clue me in on what I'm missing, or maybe a different way to accomplish this? I'm trying to get the IN clause to work because I'm thinking that it is the most efficient way to do this... Maybe it's not... Thanks for any help.
I haven't found an answer to the question about why the IN clause didn't work, but just in case anybody else has this issue I will post my workaround.
I load the parent records as usual, but my dataadapter select stmt for the child records has a "WHERE 1=0" so that zero records are initially loaded. When a new parent record is displayed I then determine if the child records for that parent are already loaded into the dataset, if not, I load them in. Bottom line is that I don't pre-load the child records, rather, I load them "on demand". This solution is working well for me.