Php, MySql join 3 tables ,and calculate SUM - php

I have 3 tables project, times and expense. I want to join all 3 of them and calculate sum as well as retrieve all the matching records.
Here are my tables:
projects:
id name
=====================
1 First Project
2 Second Project
times:
id project_id hours billed
===================================================
1 1 2.0 1
2 1 3.0 0
3 2 4.30 0
expense:
id project_id amount billed
==================================================
1 1 120.00 0
2 2 35.00 1
3 2 55.00 0
4 2 45.00 0
and here is my query:
SELECT
SUM(t.hours) as total_hours,
SUM(e.amount) as total_amount,
p.name
FROM
`projects` AS p
LEFT JOIN `expense` AS e
ON e.project_id = p.id
LEFT JOIN `times` AS t
ON t.project_id = p.id
WHERE t.billed = 0
AND e.billed = 0
GROUP BY p.id;
But for some reason I cant make it to work, I end up with no records.
The result should look something like:
Name Total Hours Total Expense
==============================================
First Project 3.00 120.00
----------------------------------------------
Second Project 7.30 100.00
----------------------------------------------

You just have the table aliases the wrong way around:
SELECT
SUM(e.hours) as total_hours,
SUM(t.amount) as total_amount,
p.name
FROM
`projects` AS p
LEFT JOIN `expense` e
ON e.project_id = p.id
LEFT JOIN `times` t
ON t.project_id = p.id
WHERE t.billed = 0
AND e.billed = 0
GROUP BY p.id;
Works as expected: http://sqlfiddle.com/#!9/48bfd1/3

Related

selecting all row from two tables and one row from last table mysql

I want to select all row from two tables and one row from last table mysql
My table is given below,
tbl_order
order_id order_no
-------- --------
1 1000
2 1001
3 1002
tbl_assign
assign_id order_id central_status
--------- -------- --------------
1 1 1
2 2 1
3 3 1
tbl_unit_status
status_id assign_id status_status
--------- --------- -------------
1 1 Work
2 2 Cutter
3 2 Stitch
4 1 Stitch
From the above 3 table, I want the result as,
order_id order_no assign_id status_status
-------- -------- --------- -------------
3 1002 3 {null}
2 1001 2 Stitch
1 1000 1 Stitch
I have tried the below code,
SELECT * FROM tbl_order o LEFT JOIN tbl_assign a ON a.order_id = o.order_id LEFT JOIN (SELECT * FROM tbl_unit_status u ORDER BY u.status_id DESC LIMIT 1) uu ON uu.assign_id = a.assign_id WHERE a.central_status = 1 ORDER BY a.assign_id DESC
But the result comes as,
order_id order_no assign_id status_status
-------- -------- --------- -------------
3 1002 3 {null}
2 1001 2 {null}
1 1000 1 Stitch
Where am doing wrong. I have tried a lot. Please help me find the answer. Thank you.
try like this:
SELECT o.*,u2.assign_id,u2.status_status FROM tbl_order o
LEFT JOIN tbl_assign a ON a.order_id = o.order_id LEFT JOIN
(SELECT u.assign_id,max(u.status_id) as maxid FROM tbl_unit_status u group by u.assign_id)
uu ON uu.assign_id = a.assign_id
LEFT JOIN tbl_unit_status u2 on u2.status_id = uu.maxid
WHERE a.central_status = 1 ORDER BY a.assign_id DESC
Try this
SELECT * FROM tbl_order o LEFT JOIN tbl_assign a ON a.order_id = o.order_id
LEFT JOIN (SELECT * FROM tbl_unit_status u ORDER BY u.status_id DESC LIMIT 2)
uu ON uu.assign_id = a.assign_id WHERE a.central_status = 1 ORDER BY
a.assign_id DESC
With LIMIT 1 you are only comparing with the last row from tbl_unit_status which has assign_id 1.

Select records from two tables if atleast 4 records in second table

I have 2 tables and i want to fetch data based on below condition:
Table1 has multiple product records.
Table2 contains various Size option for products and can not have more or less than 4 rows.
Now, i want to fetch those products which do not have entry or do not have exact 4 entries.
Table Structure is as below:
Table1
id name color price instock
----------------------------------
1 rice white 1200 1
2 shoe brown 2500 1
3 belt red 5200 1
Table2
id size pid
-----------------
1 5 1
2 10 1
3 4 1
4 15 1
5 7 2
Now Query shall fetch product with ID 2 and 3 as they have records less than 4 and no record resp.
I was using below query to fetch products which have no records in Table2
SELECT p.* FROM `Table1` p LEFT JOIN `Table2` t ON p.id = t.pid WHERE
t.pid IS NULL
SELECT p.id, p.name, p.color, p.price, p.instock, count(t.*)
FROM `Table1` p
LEFT JOIN `Table2` t
ON p.id = t.pid
GROUP BY p.id, p.name, p.color, p.price, p.instock
HAVING count(t.*) < 4

How to update single rows in duplicate entries

I have two tables t1 and t2
t1->
id line Amt
----------- ----------- -----------
1 1 0
1 2 0
2 1 0
2 2 0
2 3 0
3 3 0
3 4 0
3 5 0
4 2 0
4 3 0
--------------------------
t2->
id amt
----------- -----------
1 500
2 350
3 750
4 400
In this case I need to update t1 with amount from t2. But I need to update only one row for each id on minimum line. I can do it in MSSQL using the following query-
update a set a.amt=c.amt from T1 a inner join (
select id,min(line) line from T1 group by Id) b
on a.id=b.id and a.line=b.line
Inner join T2 c on a.id=c.Id
I want to do it in MYSQL. Is there any idea to do something like this
You need to tweak your syntax remove from clause, move set clause after joins part
update T1 a
inner join (
select id,min(line) line from T1 group by Id
) b on a.id=b.id and a.line=b.line
inner join T2 c on a.id=c.Id
set a.amt=c.amt
DEMO

MYSQL/PHP sum with group by

tableA
id B_id C_id
1 1 0
2 1 0
3 0 1
4 0 2
5 0 2
6 0 2
tableB
id amount quantity
1 10 2
tableC
id amount quantity
1 6 1
2 15 3
I have this kind of database and I know it is not structured very well because I only continued this website and I wasn't given much time to restructure the website.
My question is how can i get the total amount of tableB and tableC using a LEFT JOIN of the two tables to tableA. As you can see the quantity in tableB and tableC will make the same number of tableA record. I was able to get each of the transactions amount using this code:
SELECT *
FROM tableA A
LEFT JOIN tableB B ON A.id = B.id
LEFT JOIN tableC C ON C.id = A.id
GROUP BY B.id, C.id
It would return:
id B_id C_id B.id B.amount B. quantity C.id C.amount C.quantity
1 1 0 1 10 2 0 0 0
3 0 1 0 0 0 1 6 1
4 0 2 0 0 0 2 15 3
but now I want to get the total using mysql SUM but GROUP BY cannot be used with the aggregate function SUM so the total should be: 10+6+15 = 31
Is this what you want?
SELECT coalesce(sum(b.quantity), 0) + coalesce(sum(c.quantity), 0)
FROM tableA A LEFT JOIN
tableB B
ON A.id = B.id LEFT JOIN
tableC C
ON C.id = A.id;
This returns the total across the two tables.
EDIT:
If you just want the sum of tables b and c, what is table a for? Is this what you want?
select amountb + amountc
from (select sum(amount) as amountb from tableb) b cross join
(select sum(amount) as amountc from tablec) c;

SQL/ PHP Joining table, ignoring multiple records

I don't really know how to even search for resolution of this problem, didn't find anything specific, so here goes...
I have four tables, let's simplify them:
players
=======
id name surname
1 John Arbuckle
2 Walter White
3 Don Draper
4 Louis CK
5 Tyrion Lannister
6 Abed Nadir
sports
======
id sport
1 football
2 handball
positions
=========
id name sport_id
1 goalie 1
2 defense 1
3 attack 1
4 goalie 2
5 pivot 2
6 wing 2
7 center 2
player_position
===============
player_id position_id
1 1
1 2
1 5
2 7
2 5
3 2
4 2
5 1
5 3
6 7
6 5
So, player can play multiple sports and on multiple positions. First, I have to display list of players for a certain sport, including a column with positions they play.
What I started with is JOIN statement where I'd join those tables and have multiple rows for each player ID. That's close, but not very correct. And what I need to get is a tables like this:
FOOTBALL
ID name surname position
1 John Arbuckle PHP array(goalie,defense)
HANDBALL
ID name surname position
1 John Arbuckle PHP array(pivot)
EDIT:
So what I was looking for was GROUP_CONCAT().
Thanks guys!
select p.id, p.name, p.surname, group_concat(po.name)
from players p
inner join player_position pp on pp.player_id = p.id
inner join positions po on po.id = pp.position_id
inner join sports s on s.id = po.sport_id
where s.sport = 'football'
group by p.id
See this SQLFiddle example
For football:
SELECT players.name, players.surname, positions.name
FROM players, sports, positions, player_position
WHERE sports.id = 1 AND positions.sport_id = sports.id AND payer_position.position_id = positions.id AND players.id = player_position.player_id;
For Handball:
SELECT players.name, players.surname, positions.name
FROM players, sports, positions, player_position
WHERE sports.id = 2 AND positions.sport_id = sports.id AND payer_position.position_id = positions.id AND players.id = player_position.player_id;
I'm not sure if this is foolproof (not tested) but…
SELECT pp.`player_id` AS 'ID',s.`id` AS 'sport_id',s.`sport`,ps.`name` AS 'position',pl.`name`,pl.`surname` FROM `sports` s
INNER JOIN `positions` ps ON s.`id`=ps.`sport_id`
INNER JOIN `player_position` pp ON pp.`position_id`=ps.`id`
INNER JOIN `players` pl ON pl.`id`=pp.`player_id`
ORDER BY s.`id`,pp.`player_id`,ps.`id`
Here's a more detailed version:
SELECT
s.`id` AS 'sport_id',s.`sport`,
ps.`id` AS 'position_id',ps.`name` AS 'position',
pl.`id` AS 'player_id',pl.`name`,pl.`surname`
FROM `sports` s
INNER JOIN `positions` ps ON s.`id`=ps.`sport_id`
INNER JOIN `player_position` pp ON pp.`position_id`=ps.`id`
INNER JOIN `players` pl ON pl.`id`=pp.`player_id`
ORDER BY s.`id`,pp.`player_id`,ps.`id`

Categories