MySQL query consolidated report - mysql

I have the following tables:
customers
id name
1 customer 1
stores
id name customer_id
1 store 1 1
2 store 2 1
3 store 3 1
items
id name store_id customer_id inventory
1 item 1 1 1 10
2 item 2 2 1 12
3 item 3 2 1 18
item_attributes
id item_id store_id customer_id inventory
1 1 2 1 4
2 1 3 1 3
3 2 1 1 7
4 2 3 1 0
5 3 1 1 9
What I want is this:
item ID Item Name store 1 Store 2 Store 3
1 Item 1 10 4 3
2 Item 2 7 12 0
3 Item 3 9 18 0
A customer can have many stores.
Each store can have many items.
Other stores may or may not have the entry in the item_attributes table.
I want to get an consolidated report on item inventory.
My approach is:
Get all the items for the customer 1.
Get all the stores for the customer 1.
Get all records from the item_attributes.
foreach($i=0; $i<count($items); $i++){
foreach($j=0; $j<count($stores); $j++){
// now I get the item_id and store_id.
// check if exists in the $item_attr array with this combination.
// return the inventory if yes, 0 otherwise.
}
}
Is there any easier approach than the one I mentioned above? Any thoughts would appreciated.

Related

Count the duplicate values on multiple array and sort it to highest

I have a target where I have this DUPLICATE checker with COUNT for multiple arrays. As we can see. Any suggestions on how I make this work? Thank you and have a nice day.
DATA:
id
value
1
[5,6,8,4,2]
2
[2,3,4,1,8]
3
[9,3,2,1,10]
Normal result:
number
count
1
2
2
3
3
2
4
2
5
1
6
1
7
0
8
2
9
1
10
1
This is my target result with sorting (Highest count):
number
count
2
3
1
2
3
2
4
2
8
2
5
1
6
1
10
1
9
1

Want to display only child if it exists or else only parent table data

I just want to replace the reference id with name from item table in the result as I showed in last table
1st table and Parent table
id
item
amount
tax
status
1
4
20
2
Y
2
5
15
1
N
3
6
5
0
N
2nd table and child table
id
item
p_id
amount
tax
1
1
1
10
1
2
2
2
10
1
3
3
1
15
1
3rd table
id
item
1
mobile
2
heater
3
mouse
4
electronic
5
software
6
papers
What I get
item
amount
tax
1
10
1
2
10
1
3
15
1
5
15
1
6
5
0
What I want is
item
amount
tax
mobile
10
1
heater
10
1
mouse
15
1
software
15
1
papers
5
0
iam using this code it only works with two tables but i want to use it with three tables
SELECT IFNULL(child.item, parent.item) AS item,
IFNULL(child.amount, parent.amount) AS amount,
IFNULL(child.tax, parent.tax) AS tax
FROM parent
LEFT JOIN child ON parent.id = child.p_id

How to recursively find the children of parent in MySQL

Unable to write out the SQL Query based on complex logic defined below.
Database: MySQL
Input :
id userId parentid
----------------------
1 1 0
2 2 1
3 3 1
4 4 1
5 5 2
6 6 3
7 7 0
8 8 0
Output Expected if userId = 1 :
id userId parentid
----------------------
1 1 0
2 2 1
3 3 1
4 4 1
5 5 2
6 6 3
Logic Used:
if userId = 1 then check for parentId whose value is 1, the records are (we have to include the record of userID =1 as well)
id userId parentid
----------------------
1 1 0
2 2 1
3 3 1
4 4 1
Now in above record set check user id again in this example apart form userId = 1, there are three userId's i.e. 2, 3, 4. Now we have to see whose parent id is 2, 3 and 4. Now records are.
id userId parentid
----------------------
1 1 0
2 2 1
3 3 1
4 4 1
5 5 2
6 6 3
if we userId column again new userid visible are 5 and 6, but there is no parent id for 5 and 6. So this the final result set.

How to get total of parents categories?

I have two tables, the first is the following
"Categories"
id name parent_id
1 Food NULL
2 Vegetable 1
3 Fruit 1
4 Tomatoes 2
5 onion 2
6 bananas 3
7 grapes 3
the second table is the following
"amounts"
id amount category_id
1 100 4
2 50 6
3 25 5
4 100 7
5 50 4
6 25 7
Currently categories table are three levels and I want to return all
the Categories along with total columns:
Means that sum of the category of level 2 is the total of level 3 and
the total of level 2 should was the sum of level 1. I would really
appreciate some help Beforehand thank you very much.

I need sql query for getting the Required format

i am having table Category with the columns like
id(AutoIncrement),Parent_id,Level,Name
initially for level 1 datas has Parent_id is 0. autoincrement id will be Parent_id for next levels .my table table table datas will bw like this
id Parent_id Level Name
1 0 1 Indian
2 0 1 International
3 0 1 Nri
4 1 2 BC
5 2 2 Christian
6 2 2 Muslim
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
i want to show records in this format (its like a tree view)
id Parent_id Level Name
1 0 1 Indian
4 1 2 BC
7 4 3 MBC-1
8 7 4 OBC-2
9 1 2 FC
5 2 2 Christian
6 2 2 Muslim
2 0 1 International
3 0 1 Nri
4 1 2 BC
Can any one should help me to get this arrangement of datas using sql Query?
If it does not have a set number of branches you likely want to loop over a query in your app or write an SP to obtain all nodes. Some good reading here:
http://mikehillyer.com/articles/managing-hierarchical-data-in-mysql/