Retrieve Hierarchical Data from MySql database [duplicate] - mysql

This question already has answers here:
How to do the Recursive SELECT query in MySQL?
(6 answers)
Closed 6 years ago.
This is my Mysql Database
╔════╦═══════════╗
║ ID ║ Parent_ID ║
╠════╬═══════════╣
║ 1 ║ 0 ║
╠════╬═══════════╣
║ 2 ║ 1 ║
╠════╬═══════════╣
║ 3 ║ 1 ║
╠════╬═══════════╣
║ 4 ║ 3 ║
╠════╬═══════════╣
║ 5 ║ 4 ║
╚════╩═══════════╝
What I want to achieve :
When user search for ID 1 , I want to get all those element whose Parent_ID is 1 and also all those IDs whose have 1 as their parent or grand parent or great grand parent and so on.
OR Simply all descendants of ID 1.
Example :
if user search for 1, the algorithm should give result
{2,3,4,5} -- 4,5 because 1 is their great grand parent.
if user search for 2, the algorithm should give empty result { } as no
element have 2 as a Parent_ID
if user search for 3, the algorithm should give result {4,5} -- 5 because 3 is its grand parent.
What is the good way of saving and retrieving these type of data from database?
I am using Java and MySQL.
Thanks.

Seeing your data model, you can't do it in a single SQL query, since you need an unknown level of recursion. You need to create a function or stored procedure to iterate over the results of subsequent queries.
This answer may help you:
https://dba.stackexchange.com/questions/30021/mysql-tree-hierarchical-query
[Edit]
If you always query for a root parent (i.e. a row that may have children but has no parent), you can store that id in each row and query by that column:
╔════╦═══════════╦════════════════╗
║ ID ║ Parent_ID ║ Root_Parent_ID ║
╠════╬═══════════╬════════════════╣
║ 1 ║ 0 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 2 ║ 1 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 3 ║ 1 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 4 ║ 3 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 5 ║ 4 ║ 1 ║
╠════╬═══════════╬════════════════╣
║ 6 ║ 0 ║ 6 ║
╠════╬═══════════╬════════════════╣
║ 7 ║ 6 ║ 6 ║
╠════╬═══════════╬════════════════╣
║ 8 ║ 7 ║ 6 ║
╚════╩═══════════╩════════════════╝
Then you can just query:
SELECT * FROM mytable WHERE Root_Parent_ID = 1
Otherwise, I recommend you to go more in depth on how to store this data more efficiently: https://blogs.msdn.microsoft.com/anthonybloesch/2006/02/15/hierarchies-trees-in-sql-server-2005/

Related

MySQL Query Grouped Conditional Count

Here is the mySQL table data:
╔════╦══════╦══════════╦══════════════╗
║ ID ║ USER ║ DATE ║ NUMDOWNLOADS ║
╠════╬══════╬══════════╬══════════════╣
║ 1 ║ John ║ xx-xx-xx ║ 1 ║
║ 2 ║ Mary ║ xx-xx-xx ║ 3 ║
║ 3 ║ John ║ xx-xx-xx ║ 5 ║
║ 4 ║ Mary ║ xx-xx-xx ║ 2 ║
║ 5 ║ Mary ║ xx-xx-xx ║ 6 ║
║ 6 ║ John ║ xx-xx-xx ║ 7 ║
║ 7 ║ John ║ xx-xx-xx ║ 1 ║
║ 8 ║ Mary ║ xx-xx-xx ║ 8 ║
║ 9 ║ Mary ║ xx-xx-xx ║ 9 ║
╚════╩══════╩══════════╩══════════════╝
What I want to accomplish is to group the data by USER, and display the total NUMDOWNLOADS per USER where NUMDOWNLOADS is > X. For example, if X=5:
John: 1 (since 1 NUMDOWNLOADS > 5, and others count collectively as 1)
Mary: 3 (since 3 NUMDOWNLOADS > 5, and others count collectively as 1)
So, (1) output per user, and (2) output total, which in this case would be 4. Clear as mud :) Ideas on statement to use?
Your query is here. Try it
SELECT USER, COUNT(NUMDOWNLOADS)
FROM table_name
WHERE NUMDOWNLOADS > 5
GROUP BY USER
SELECT USER, COUNT(NUMDOWNLOADS)
FROM downloads
WHERE NUMDOWNLOADS > 5
GROUP BY USER
Follow the link below for a running demo:
SQLFiddle
I think you just want to count records where NUMDOWNLOADS > 5:
select USER, count(*)
from myTable
where NUMDOWNLOADS > 5
group by USER
The WHERE filter is performed before any grouping is done, so first this query filters out any rows that do not match NUMDOWNLOADS > 5, then it groups by USER and counts.
Alternatively if there is something about your actual query that requires you to use a conditional sum, you can do so as well:
select USER, sum(case when NUMDOWNLOADS > 5 then 1 else 0 end)
from myTable
group by USER

How to get all items and their value by mysql query

I have three mysql tables.
Table-01: table_item
In this table, all items with their corresponding ids are stored. Items are categorized in two categories. One is writing category (catid-1) and another is clothing category (catid-2).
---------------------------------------------
| id | catname | catid | itemid | itemname |
---------------------------------------------
║ 1 ║ writing ║ 1 ║ 1 ║ Pen ║
║ 2 ║ writing ║ 1 ║ 2 ║ Pencil ║
║ 3 ║ writing ║ 1 ║ 3 ║ Sharpner ║
║ 4 ║clothing ║ 2 ║ 4 ║ Pant ║
║ 5 ║clothing ║ 2 ║ 5 ║ shirt ║
║ 6 ║clothing ║ 2 ║ 6 ║ coat ║
║ 7 ║clothing ║ 2 ║ 7 ║ Tie ║
---------------------------------------------
Table-02: bid_item
Each year bid/tender is called for purchasing selective items from the above item table (table_item). So in this table, selected items, those are selected for bid/tender, are stored per year basis.
----------------------
║ id ║ itemid ║ year ║
----------------------
║ 1 ║ 1 ║ 2015 ║
║ 2 ║ 2 ║ 2015 ║
║ 3 ║ 3 ║ 2015 ║
║ 4 ║ 4 ║ 2015 ║
║ 5 ║ 5 ║ 2015 ║
║ 6 ║ 6 ║ 2015 ║
║ 7 ║ 1 ║ 2016 ║
║ 8 ║ 2 ║ 2016 ║
║ 9 ║ 3 ║ 2016 ║
║ 10 ║ 4 ║ 2016 ║
║ 11 ║ 7 ║ 2016 ║
----------------------
Table-03: bid_2015
After calling bid/Tender, rate of the corresponding companies for selected items are stored in this table. Each company will not give bid price for each items selected for a particular year. Tables are created according to the year alias. Here bid rate for year 2015 are stored as below:
--------------------------------
║ id ║ itemid ║ company ║ rate ║
--------------------------------
║ 1 ║ 1 ║ X ║ 2.0 ║
║ 2 ║ 2 ║ X ║ 2.2 ║
║ 3 ║ 3 ║ X ║ 1.0 ║
║ 4 ║ 4 ║ X ║ 10.0 ║
║ 5 ║ 5 ║ X ║ 15.0 ║
║ 6 ║ 1 ║ Y ║ 1.5 ║
║ 8 ║ 2 ║ Y ║ 2.0 ║
║ 9 ║ 3 ║ Y ║ 1.5 ║
║ 10 ║ 4 ║ Y ║ 12.0 ║
║ 11 ║ 6 ║ Y ║ 20.0 ║
--------------------------------
I need a html table (with input field) for data entry/edit for a particular year (Here, for 2015). Table will contain following:
1) Table will show each items for clothing catagory (catid-2)those are declared for the bid/tender for 2015.
2) If a company bids price, price will be in rate column, otherwise rate column will be blank. Administrator can either change the price (if entered wrong bid price) or add rate for other items which was not entered at first for a particular table.
So the look of the html table form will be as below:
HTML Form-Table (For Data Entry/Edit) : For Company-Y and clothing catagory for the year-2015
Company: (drop-down menu- Company-Y)
------------------------------------------------------
Item Name | Rate |
----------------------
Pant | 12.0 |
-----------------------
Shirt |
-----------------------
Coat | 20.0 |
-----------------------
============
Submit
============
As you can see Rate of Shirt is blank for Company-Y as this company did not give bid price for Shirt. If this company give the price of Shirt at later, rate can be entered and can update the table.
So what would be mysql query to get a html form table like above?
If I execute this query:
SELECT i.itemname,d.rate FROM table_item as i INNER JOIN bid_item as b ON i.itemid=b.itemid LEFT JOIN bid_2015 as d ON b.itemid=d.itemid WHERE i.catid=2 AND d.company='Y' AND b.year=2015
But the expected html table is not coming. As INNER and LEFT Join are used, Item Names are shown more than once in the form table.
What will be the effective mysql query to output a html form table like above?
this works for all data of company Y in 2015
select itemname,
rate
from table_item join bid_item on(table_item.itemid=bid_item.itemid and year=2015 and catid=2)
left join bid_2015 on( bid_2015.itemid=bid_item.itemid and
bid_item.itemid=table_item.itemid and company='Y' and year=2015 and catid=2)
This query will work fine.
SELECT i.itemname,
d.rate
FROM table_item as i
INNER JOIN bid_item as b ON i.itemid=b.itemid AND b.year=2015
LEFT JOIN bid_2015 as d ON b.itemid=d.itemid AND d.company='Y'
WHERE i.catid=2
Note:
When ever you are making LEFT/RIGHT joins on a table (say b as in
your example below). Never put conditions of table 'b' in WHERE
clause. It will act as INNER JOIN .
So the this code
SELECT i.itemname,
d.rate
FROM table_item as i
INNER JOIN bid_item as b ON i.itemid=b.itemid
LEFT JOIN bid_2015 as d ON b.itemid=d.itemid
WHERE i.catid=2 AND d.company='Y' AND b.year=2015
Act as INNER JOIN as given below.
SELECT i.itemname,
d.rate
FROM table_item as i
INNER JOIN bid_item as b ON i.itemid=b.itemid
INNER JOIN bid_2015 as d ON b.itemid=d.itemid
WHERE i.catid=2 AND d.company='Y' AND b.year=2015
Hope this helps.

De-duplicating many-to-many relationships in MySQL lookup table

I've inherited a database that includes a lookup table to find other patents that are related to a given patent.
So it looks like
╔════╦═══════════╦════════════╗
║ id ║ patent_id ║ related_id ║
╠════╬═══════════╬════════════╣
║ 1 ║ 1 ║ 2 ║
║ 2 ║ 1 ║ 3 ║
║ 3 ║ 2 ║ 1 ║
║ 4 ║ 2 ║ 3 ║
║ 5 ║ 3 ║ 2 ║
╚════╩═══════════╩════════════╝
And I want to filter out the reciprocal relationships. 1->2 and 2->1 are the same for my purposes so I only want 1->2.
I don't need to make the edit in the table, I just need a query the returns a list of the unique relationships, and while I'm sure it's simple I've been banging my head against the keyboard for far too long.
Here is a clever query which you can try using. The general strategy is to identify the unwanted duplicate records and then subtract them away from the entire set.
SELECT t.id, t.patent_id, t.related_id
FROM t LEFT JOIN
(
SELECT t1.patent_id AS t1_patent_id, t1.related_id AS t1_related_id
FROM t t1 LEFT JOIN t t2
ON t1.related_id = t2.patent_id
WHERE t1.patent_id = t2.related_id AND t1.patent_id > t1.related_id
) t3
ON t.patent_id = t3.t1_patent_id AND t.related_id = t3.t1_related_id
WHERE t3.t1_patent_id IS NULL
Here is the inner temporary table generated by this query. You can convince yourself that by applying the logic in the WHERE clause you will select the correct records. Non-duplicate records are characterized by t1.patent_id != t2.related_id, and all these records are retained. In the case of duplicates (t1.patent_id = t2.related_id), the record chosen from each pair of duplicates is the one where patent_id < related_id, as you requested in your question.
╔════╦══════════════╦═══════════════╦══════════════╦═══════════════╗
║ id ║ t1.patent_id ║ t1.related_id ║ t2.patent_id ║ t2.related_id ║
╠════╬══════════════╬═══════════════╬══════════════╬═══════════════╣
║ 1 ║ 1 ║ 2 ║ 2 ║ 1 ║ * duplicate
║ 1 ║ 1 ║ 2 ║ 2 ║ 3 ║
║ 2 ║ 1 ║ 3 ║ 3 ║ 2 ║
║ 3 ║ 2 ║ 1 ║ 1 ║ 2 ║ * duplicate
║ 3 ║ 2 ║ 1 ║ 1 ║ 3 ║
║ 4 ║ 2 ║ 3 ║ 3 ║ 2 ║ * duplicate
║ 5 ║ 3 ║ 2 ║ 2 ║ 1 ║
║ 5 ║ 3 ║ 2 ║ 2 ║ 3 ║ * duplicate
╚════╩══════════════╩═══════════════╩══════════════╩═══════════════╝
Click the link below for a running example of this query.
SQLFiddle
Try something like
select distinct * from
(select patient_id, related_id from TABLENAME
union
select related_id, patient_id from TABLENAME
);
Okay you're right the above won't work. Try
select patient_id, related_id from TABLENAME p1
where p1.patiend_id not in
(select patient_id from TABLENAME p2
where p2.related_id = p1.related_id)

MySQL - Merge rows in table based on multiple criteria

I'd like to merge rows based on multiple criteria, essentially removing duplicates where I get to define what "duplicate" means. Here is an example table:
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 1 ║ John ║ 11 ║ 5 ║
║ 2 ║ John ║ 11 ║ 5 ║
║ 3 ║ John ║ 11 ║ 6 ║
║ 4 ║ Sam ║ 14 ║ 7 ║
║ 5 ║ Sam ║ 14 ║ 7 ║
╚═════╩═══════╩═════╩═══════╝
In my example, let's say I want to merge on name and age but ignore grade. The result should be:
╔═════╦═══════╦═════╦═══════╗
║ id* ║ name ║ age ║ grade ║
╠═════╬═══════╬═════╬═══════╣
║ 1 ║ John ║ 11 ║ 5 ║
║ 3 ║ John ║ 11 ║ 6 ║
║ 4 ║ Sam ║ 14 ║ 7 ║
╚═════╩═══════╩═════╩═══════╝
I don't particularly care if the id column is updated to be incremental, but I suppose that would be nice.
Can I do this in MySQL?
My suggestion, based on my above comment.
SELECT distinct name, age, grade
into tempTable
from theTable
This will ignore the IDs and give you only a distinct dump, and into a new table.
Then you can either drop the old and, and rename the new one. Or truncate the old one, and dump this back in.
You could just delete the duplicates in place like this:
delete test
from test
inner join (
select name, age, grade, min(id) as minid, count(*)
from test
group by name, age, grade
having count(*) > 1
) main on test.id = main.minid;
Example: http://sqlfiddle.com/#!9/f1a38/1

Update a table by inserting a count of foreign key from another table

I have two tables:
╔════════════════╗ ╔════════════════╗
║ ITEM ║ ║ ITEM_TRACK ║
╠════════════════╣ ╠════════════════╣
║ ID ║ ║ ID ║
║ GUID ║ ║ ITEM_GUID ║
║ COUNT1 ║ ║ CONTEXT ║
║ ENDDATE ║ ║ ║
╚════════════════╝ ╚════════════════╝
╔═════╦══════╦════════╗ ╔═════╦═══════════╦══════════╗
║ ID ║ GUID ║ COUNT1 ║ ║ ID ║ ITEM_GUID ║ CONTEXT ║
╠═════╬══════╬════════╣ ╠═════╬═══════════╬══════════╣
║ 1 ║ aaa ║ ║ ║ 1 ║ abc ║ ITEM ║
║ 2 ║ bbb ║ ║ ║ 2 ║ aaa ║ PAGE ║
║ 3 ║ ccc ║ ║ ║ 3 ║ bbb ║ ITEM ║
║ 4 ║ abc ║ ║ ║ 4 ║ ccc ║ ITEM ║
╚═════╩══════╩════════╝ ║ 5 ║ abc ║ ITEM ║
║ 6 ║ aaa ║ ITEM ║
║ 7 ║ abc ║ ITEM ║
║ 8 ║ ccc ║ PAGE ║
╚═════╩═══════════╩══════════╝
What I'm trying to do is fill in the COUNT1 column in ITEM with the count of the number of times ITEM_GUID appears in ITEM_TRACK for all ITEM.GUIDs where ENDDATE is still in the future. I need to do this once an hour for all GUIDS in ITEM.
I can get the counts I need easily
SELECT ITEM_GUID, COUNT(*) from ITEM_TRACK GROUP BY ITEM_GUID;
What I don't know how to do is, how do I merge this with an INSERT INTO statement to automatically update all the items in the items table with the count based on their ENDDATE?
UPDATE:
I have a working solution based on Aquillo's answer:
UPDATE ITEM a
SET COUNT1 = (SELECT COUNT(*) AS total FROM ITEM_TRACK b WHERE b.item_guid=a.guid);
Is there any other way to do this without a subquery?
You can insert from a select like this:
INSERT INTO myTable (foreignKey, countColumn) VALUES
SELECT ITEM_GUID, COUNT(*) from ITEM_TRACK GROUP BY ITEM_GUID;
In case you want to update, try something like this:
UPDATE from SELECT using SQL Server
If you use INSERT INTO you'll put additional rows in your ITEM table, not update the existing ones. If this is what you meant then that's great, but if you want to update the existing ones, you'll need to use update. You do this by joining the table you want to update with the table you want to update from. However, in your case you want to update from an aggregation and so you need to create a table with the aggregated values. Try this:
UPDATE ITEM SET Count1 = temp.total
FROM Item
INNER JOIN (
SELECT ITEM_GUID, COUNT(*) AS total
FROM ITEM_TRACK
GROUP BY ID) AS temp
ON Item.GUID = temp.ITEM_GUID
WHERE ENDDATE > NOW()
I've tried this on SQL Server (using GETDATE() instead of NOW()) to double check and it worked, I think it should work on MYSQL.