PHP - Counting some values in column hieararchy - php

i need help to calculate a value inside a hierarchy array. I have a table like this:
+--------+------------+-------+-----------+
| kia_id | kia_name | value | parent_id |
+--------+------------+-------+-----------+
| 1 | ac service | | 0 |
| 2 | hil | | 0 |
| 3 | dispatch | | 1 |
| 4 | tat main | 13.3 | 3 |
| 5 | tat air | 10.1 | 3 |
| 6 | sla comp | 11.7 | 2 |
| 7 | sla serv | | 2 |
| 8 | slb | 9.9 | 7 |
+--------+------------+-------+-----------+
i want to display to html that table like this :
+--------+----------------+-------+-----------+------------------------+
| kia_id | kia_name | value | parent_id | total_value_from_child |
+--------+----------------+-------+-----------+------------------------+
| 1 | ac service | | 0 | 23.4 |
| 3 | dispatch | | 1 | 23.4 |
| 4 | tat main | 13.3 | 3 | |
| 5 | tat air | 10.1 | 3 | |
| 2 | hil | | 0 | 21.6 |
| 6 | sla comp | 11.7 | 2 | |
| 7 | sla serv | | 2 | 9.9 |
| 8 | slb | 9.9 | 7 | |
+--------+----------------+-------+-----------+------------------------+
how to count all child value to set the total of this parent, please help me..

I think you need to combine a recursive CTE with aggregation. The idea is to start at the "bottom" (leafs) and work your way up. Then aggregate to bring in the original data.
with cte as (
select kia_id, value, kia_id as parent_id, 0 as lev
from t
where not exists (select 1 from t t2 where t2.parent_id = t.kia_id)
union all
select cte.kia_id, cte.kia_value, t.parent_id, cte.lev + 1
from cte join
t
on cte.parent_id = t.kia_id
)
select t.*, c.parent_value
from (select cte.parent_id, sum(kia_value) as parent_value
from cte
group by cte.parent_id
) c join
t
on t.kia_id = c.parent_id;

Related

SQL select count and sum from different tables

I need help to resolve this.
I have 4 tables :
Transactions
| id | cid | gt | rt |
| 1 | 6 | 2 | 5 |
| 2 | 6 | 9 | 7.5 |
| 3 | 6 | 3 | 9.7 |
| 4 | 3 | 3 | 7.0 |
| 5 | 3 | 7 | 6.8 |
| 6 | 9 | 4 | 2.5 |
| 7 | 9 | 2 | 5.4 |
Clients
| id | firstname | lastname | date |
| 1 | jean | moulin | 1987 |
| 2 | salah | fera | 1968
| 3 | marouan | youra | 2001 |
| 4 | amin | esa | 1963 |
| 5 | kamal | tara | 1789 |
| 6 | moad | mara | 2005 |
| 9 | safaa | dara | 2004 |
Produit A
| id | cid |
| 1 | 6 |
| 2 | 6 |
| 3 | 3 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 1 |
Produit B
| id | cid |
| 1 | 6 |
| 2 | 3 |
| 3 | 9 |
| 4 | 3 |
| 5 | 3 |
| 6 | 4 |
| 7 | 6 |
The result that i need is :
cid | name | date | pa | pb | gt | rt |
3 | | | | | | |
6 | | | | | | |
9 | | | | | | |
I need to select from transaction all distinct client id (pid) and select the firstname and last name (name = firstname lastname) and date from clients table and sum all values (gt) and (rt) and search in table produitA the number of products for this client by his id and the same thing for the table produitB.
What i do for this but it don't work is (suggested by Gimeniux):
SELECT
clients.id,
CONCAT(firstname, ' ', lastname) as name,
date,
count(distinct produitA.id) as pa,
count(distinct produitB.id) as pb,
sum(gt) AS gt,
sum(rt) AS rt
FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid
where pid is not null
group by clients.id
The probleme here is that gt and rt values are true for only the first client. For the second client and third and ... there is different values that are not true.
Although is hard for me to see the logic between your tables, you can use this query to get the result you desire. But i think it won't work if there are two same 'gt' or two same 'rt' values for one client.
SELECT
clients.id,
CONCAT(firstname, ' ', lastname) as name,
date,
count(distinct produitA.id) as pa,
count(distinct produitB.id) as pb,
sum(distinct gt) AS gt,
sum(distinct rt) AS rt
FROM clients
LEFT JOIN transactions ON clients.id = transactions.pid
LEFT JOIN produitA ON clients.id = produitA.cid
LEFT JOIN produitB ON clients.id = produitB.cid
where pid is not null
group by clients.id
Row for pid=9 doesn't show because in the data you gave there is no client with id=9

MySQL get Related and Unrelated Records from Table in One Query

I have a user and pages table, and these two have many to many relationship between them with the bridge table user privileges. What I want to get all the pages name and only those are marked that is assigned to that user.
want some think like this
+---------+----------------------------------+
| user_id | page_name | Assigned|
+---------+----------------------------------+
| 1 | Add Project | 0 |
| 1 | Department | 0 |
| 1 | Category | 1 |
| 1 | Item | 0 |
| 1 | Units | 1 |
| 1 | Stock In | 0 |
| 1 | Stock Card Report | 1 |
+---------+------------------------+---------|
for now my query is this;
Select up.user_id, p.page_name FROM user_privileges up, pages p where p.page_id = up.page_id and up.user_id = 1;
and it returns this;
+---------+------------------------+
| user_id | page_name |
+---------+------------------------+
| 1 | Add Project |
| 1 | Department |
| 1 | Category |
| 1 | Item |
| 1 | Units |
| 1 | Stock In |
| 1 | Stock Card Report |
+---------+------------------------+
The Scheme is this;
table - user
+---------+-----------+
| user_id | user_name |
+---------+-----------+
| 4 | saif |
| 1 | admin |
| 5 | taqi |
| 2 | rashid |
+---------+-----------+
table - pages
+---------+---------------+
| page_id | page_name |
+---------+---------------+
| 2 | Page 1 |
| 3 | Page 2 |
| 5 | Page 3 |
| 6 | Page 4 |
| 7 | Page 5 |
| 8 | Page 6 |
| 9 | Page 7 |
| 10 | Page 8 |
| 11 | Page 9 |
| 13 | Page 10 |
| 14 | Page 11 |
| 15 | Page 12 |
| 16 | Page 13 |
| 18 | Page 14 |
| 19 | Page 15 |
| 20 | Page 16 |
+---------+---------------+
and table user_privalges for user_id = 1 only.
+--------------------+---------+---------+
| user_privileges_id | user_id | page_id |
+--------------------+---------+---------+
| 1 | 1 | 2 |
| 2 | 1 | 3 |
| 3 | 1 | 5 |
| 4 | 1 | 6 |
| 5 | 1 | 7 |
| 6 | 1 | 8 |
| 7 | 1 | 9 |
| 8 | 1 | 10 |
| 9 | 1 | 11 |
| 10 | 1 | 13 |
| 11 | 1 | 14 |
| 12 | 1 | 15 |
| 13 | 1 | 16 |
| 14 | 1 | 18 |
| 15 | 1 | 19 |
| 16 | 1 | 20 |
+--------------------+---------+---------+
Select up.user_id, p.page_name FROM user_privileges up, pages p where p.page_id = up.page_id and up.user_id = 1 AND up.Assigned=1;
"What I want to get all the pages name and only those are marked that is assigned to that user." So with your edit you should try something like that with case statement
SELECT up.user_id, p.page_name,
CASE
WHEN up.page_id=p.page_id THEN '1'
ELSE '0'
END AS Assigned
FROM pages p left join user_privileges up
ON p.page_id = up.page_id
WHERE up.user_id = 1;
If you are only looking for the user_id = 1 you may do something as
select
1 as user_id,
p.page_name,
case when up.page_id is not null then 1 else 0 end as `Assigned`
from pages p
left join user_privalges up on up.page_id = p.page_id and up.user_id = 1
order by p.page_id
http://www.sqlfiddle.com/#!9/06f65/5

Concatenate and Count the multiple rows in single rows in mysql

I want to concatenate and count data of the same column, so I can concatenate but I can not count the repeated data.
Here's my table of data:
| ID | bills | class |
|-----|-------|-------|
| 1 | 0.5 | 2 |
| 2 | 1 | 1 |
| 3 | 0.5 | 2 |
| 5 | 1 | 3 |
| 6 | 0 | 2 |
| 7 | 0.5 | 1 |
| 8 | 1 | 2 |
| 9 | 1 | 3 |
| 10 | 0.5 | 1 |
| 11 | 0 | 2 |
| 12 | 1 | 1 |
| 13 | 0 | 3 |
| 14 | 1 | 2 |
| 15 | 0 | 1 |
| 16 | 0 | 1 |
| 17 | 0.5 | 3 |
| 18 | 0 | 3 |
| 13 | 0.5 | 3 |
Here's my sql query I'm using to concatenate data:
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
FROM tb_presence
GROUP BY class;
Here's my result below:
| class | bills |
|-------|------------------|
| 1 | 1,0.5,0.5,1,0,0 |
| 2 | 0.5,0,1,0,1 |
| 3 | 1,1,0,0.5,0,0.5 |
Now I would like to count the data that are equal, but continue with the same concatenation.
I want to "count" the data with the same values ​​and display concatenated (column observation and only to help understanding)
| class | bills | observation |
|-------|-------|-----------------------------|
| 1 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
| 2 | 2,1,2 | (2=0+0) (1=0.5) (2=1+1) |
| 3 | 2,2,2 | (2=0+0) (2=0.5+0.5) (2=1+1) |
Is this really possible?
Here is a solution (thanks to #wchiquito for the sqlfiddle) See http://sqlfiddle.com/#!2/2d2c8/1
As you can see it cannot dynamically determine the bills' values and count them. But there is a count per bill value that you want.
SELECT class AS lesson,
GROUP_CONCAT( bills ORDER BY bills ) AS bills
,SUM(IF(bills=0,1,0)) AS Count0
,SUM(IF(bills=0.5,1,0)) AS Count05
,SUM(IF(bills=1,1,0)) AS Count1
,COUNT(*) AS totalRecords
,COUNT(*)
- SUM(IF(bills=0,1,0))
- SUM(IF(bills=0.5,1,0))
- SUM(IF(bills=1,1,0))
AS Missing
FROM tb_presence GROUP BY class;
I added an extra record to show how the 'missing' column could show if you were not taking all values into consideration.
Results
| LESSON | BILLS | COUNT0 | COUNT05 | COUNT1 | TOTALRECORDS | MISSING |
|--------|-----------------------------------------|--------|---------|--------|--------------|---------|
| 1 | 0.00,0.00,0.50,0.50,1.00,1.00,1.00,4.00 | 2 | 2 | 3 | 8 | 1 |
| 2 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |
| 3 | 0.00,0.00,0.50,0.50,1.00,1.00 | 2 | 2 | 2 | 6 | 0 |

Selecting Total value of items from 2 tables and update value of another TABLE (COMPLICATED QUERY)

My case is that I want to compute a player atk power to be used on a battle module that i made, but just wondering i have actually 2 options:
Calculate Damage Dealt from the server.(my CURRENT OPTIONS)
use PHP to calculate DAMAGE DEALT and UPDATE server DATABASE values.
pass 2 of the chara id and just calculate all in the QUERY and UPDATE all(is this possible).
Question: Can I do it in a query?(option B)
my current set-up:
1 character has 4 items and i compute the characters atk by adding all 4 of the item atk and the chara base atk in the client side. (which i think is prone to security holes)
and then update the values in the server side.
Here is my tables:
chara:
+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+
| chara_id | chara_name | chara_class_id | chara_level | chara_gold | chara_hp | chara_mp | chara_xp | chara_atk | chara_def |
+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+
| 1 | LawrenceX | 1 | 5 | 230 | -175 | 1000 | 0 | 7 | 3 |
| 3 | Viscocent | 2 | 2 | 96 | -206 | 1100 | 1700 | 5 | 5 |
| 4 | Piatos | 1 | 1 | 120 | -60 | 1000 | 0 | 7 | 3 |
| 5 | Hello | 1 | 1 | 300 | -50 | 1000 | 200 | 2 | 8 |
| 6 | Sample | 3 | 2 | 251 | -85 | 900 | 0 | 9 | 1 |
| 8 | Sampuro | 2 | 1 | 170 | 895 | 1100 | 700 | 5 | 5 |
| 12 | fail | 2 | 3 | 481 | 1100 | 1300 | 0 | 21 | 9 |
| 13 | new | 1 | 1 | 1000 | -80 | 1000 | 0 | 5 | 5 |
+----------+------------+----------------+-------------+------------+----------+----------+----------+-----------+-----------+
items:
+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-------------------------------------------------+------------+
| 0 | None | 0 | 0 | 0 | 0 | pics/none.png | | 400 |
| 1 | Axe | 1 | 220 | 10 | 0 | pics/weapons/axe.png | Another lumberjack axe is another man's weapon. | 200 |
| 2 | Wooden Sword | 1 | 70 | 0 | 0 | pics/weapons/wooden-sword.png | A wooden sword, 99% made from wood | 225 |
| 3 | Dagger | 1 | 60 | 5 | 0 | pics/weapons/dagger.png | A Dagger, Cheap and Sharp | 55 |
| 4 | Bow | 1 | 120 | 1 | 0 | pics/weapons/bow.png | The basics and simplest of all bows. | 120 |
| 5 | Helmet | 4 | 0 | 50 | 0 | pics/headgears/helmet.png | iron helmet - made from an iron pot scraps. | 155 |
| 6 | Tunic | 2 | 10 | 10 | 0 | pics/armors/tunic.png | A peasants tunic. | 50 |
| 7 | Armour | 2 | 0 | 75 | 0 | pics/armors/armour.png | | 150 |
| 8 | Necklace | 3 | 25 | 15 | 0 | pics/accessories/necklace.png | | 199 |
| 9 | Studded Leather | 2 | 25 | 60 | 0 | pics/armors/studded-leather.png | | 240 |
+---------+-----------------+-----------+----------+----------+----------+---------------------------------+-------------------------------------------------+------------+
equipment:
+----------+----------+-----------+-------------+----------+---------+
| equip_id | chara_id | weapon_id | headgear_id | armor_id | ring_id |
+----------+----------+-----------+-------------+----------+---------+
| 3 | 1 | 14 | 5 | 6 | 8 |
| 5 | 3 | 4 | 5 | 6 | 8 |
| 6 | 4 | 11 | 5 | 7 | 8 |
| 7 | 5 | 12 | 5 | 6 | 8 |
| 8 | 6 | 3 | 16 | 7 | 8 |
| 10 | 8 | 15 | 5 | 7 | 8 |
| 13 | 12 | 14 | 5 | 6 | 17 |
| 40 | 13 | 3 | 5 | 7 | 8 |
+----------+----------+-----------+-------------+----------+---------+
table relationships:
1 chara = 1 equipment
1 weapon_id, armor_id, ring_id, headgear_id = 1 item (total of 4 items, headgear_id = 1 item).
I CAN GET THE EQUIPMENTS OF A CHARACTER BY USING THIS QUERY(KUDOS #JC):
SELECT i1.item_atk weapon_atk,i1.item_def weapon_def,
i2.item_atk headgear_atk,
i2.item_def headgear_def,
i3.item_atk armor_atk,
i3.item_def armor_def,
i4.item_atk ring_atk,
i4.item_def ring_def
FROM equipment e LEFT JOIN
item i1 ON e.weapon_id = i1.item_id LEFT JOIN
item i2 ON e.headgear_id = i2.item_id LEFT JOIN
item i3 ON e.armor_id = i3.item_id LEFT JOIN
item i4 ON e.ring_id = i4.item_id
WHERE e.chara_id = 1
RESULTS:
+------------+------------+--------------+--------------+-----------+-----------+----------+----------+
| weapon_atk | weapon_def | headgear_atk | headgear_def | armor_atk | armor_def | ring_atk | ring_def |
+------------+------------+--------------+--------------+-----------+-----------+----------+----------+
| 275 | 25 | 0 | 50 | 10 | 10 | 25 | 15 |
+------------+------------+--------------+--------------+-----------+-----------+----------+----------+
now i want to total the atk and def of that character equipment and return it in that query
expected results:
+------------+------------+
| total_atk | total_def |
+------------+------------+
| 310 | 100 |
+------------+------------+
this is the simplest way that I can think of.
SELECT IFNULL(W.item_atk, 0) + IFNULL(H.item_atk, 0) + IFNULL(A.item_atk, 0) + IFNULL(R.item_atk, 0) AS total_atk
, IFNULL(W.item_def, 0) + IFNULL(H.item_def, 0) + IFNULL(A.item_def, 0) + IFNULL(R.item_def, 0) AS total_def
FROM equipment E
LEFT JOIN item W ON W.item_id = E.weapon_id
LEFT JOIN item H ON H.item_id = E.headgear_id
LEFT JOIN item A ON A.item_id = E.armor_id
LEFT JOIN item R ON R.item_id = E.ring_id
WHERE E.chara_id = 1
I have renamed the aliases of tables to track them easily. And I used IFNULL in case the character has no particular equipment.
==================================================================================
Dude, I just made another query, I think this is faster than the one above. Though, I haven't tested them.
SELECT SUM(IFNULL(I.item_atk, 0)) AS total_atk
, SUM(IFNULL(I.item_def, 0)) AS total_def
FROM equipment E
LEFT JOIN item I ON I.item_id = E.weapon_id
OR I.item_id = E.headgear_id
OR I.item_id = E.armor_id
OR I.item_id = E.ring_id
WHERE E.chara_id = 1
GROUP BY E.chara_id

mysql inner join 2 tables and order by count

I am having the following tables in my DB
PROJECTS
+----+-------------------------------------------+
| id | name |
+----+-------------------------------------------+
| 1 | YANNONALI COURT |
| 2 | UNIVERSITY OF COLORARDO DENVER RESEARCH 2 |
| 3 | G.R.E.A.T PROGRAM DESALTER BUILDING |
| 4 | MONARCH CLUB |
| 5 | LAFAYETTE MERCANTILE |
| 6 | CAMELBACK VILLAGE RAQUET AND HEALTH CLUB |
| 7 | BACK COUNTRY |
| 8 | URBAN CRASHPAD |
| 9 | PRIVATE RESIDENCE |
| 10 | EATON RESIDENCE |
+----+-------------------------------------------+
PROJECT_ASSIGNMENTS(WHERE projects.id=project_assignment.target_id)
+-------+-----------+-------------+
| id | target_id | property_id |
+-------+-----------+-------------+
| 19178 | 1 | 48 |
| 19192 | 1 | 39 |
| 19391 | 1 | 3 |
| 19412 | 2 | 3 |
| 19591 | 2 | 34 |
| 19610 | 2 | 34 |
| 21013 | 3 | 2 |
| 21032 | 3 | 2 |
| 30876 | 4 | 2433 |
| 38424 | 5 | 2580 |
+-------+-----------+-------------+
PROPERTIES(WHERE properties.id= project_assignment.property_id)
+----+------------------+
| id | name |
+----+------------------+
| 2 | Residential |
| 3 | Multi Family |
| 34 | New Construction |
| 39 | Contemporary |
| 48 | Southwest |
+----+------------------+
I want O/P ordered by no.of projects in the list...
Residential(177) //12 - total no.of projects which is having this property
Multi Family(15)
New Construction(13)
Contemporary(11)
please give me some MySQL queries
Thank You
This should do the trick:
select
c.name,
count(c.id) as CountOfProperties
from
projects a,
project_assignments b,
properties c
where
a.ID=b.target_id
and b.property_id=c.ID
group by
c.name
order by
count(c.id) desc;
Try this::
select
prop.name,
count(prop.id) as CountOfProperties
from
projects p
inner join project_assignments pa on (p.ID=pa.target_id)
inner join properties prop on (pa.property_id=prop.ID)
group by
prop.name
order by
count(prop.id) desc;

Categories