how to avoid sub query on mysql query - php

there is 4 table
team
---------------
id | teamname |
1 | lal |
2 | sobuj |
member
-----------------------
id | membername| team |
1 | sagor | 1 |
2 | sumon | 1 |
dps
---------------------------------------------------
id | member| team | deposit | refund | ddate |
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
mvs
----------------------------------------------------
id | member| team | mvs_total| refund | ddate |
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
here is my query:
$result=mysql_query(select * from member where team='1');
$row=mysql_fetch_array($result);
do{
$aresult=mysql_query(select sum(deposit)-sum(refund)as balance from dps where member='$row[id]');
$arow=mysql_fetch_array($aresult);
echo $arow[balance];
$bresult=mysql_query(select sum(mvs_total)as mvs_balance from mvs where member='$row[id]' and date1<='2016-09-01');
$brow=mysql_fetch_array($bresult);
echo $brow[mvs_total];
} while($row=mysql_fetch_array($result));
its taking too much time how to avoid sub query or how can i save time advice plz Thanks in advance.

First thing I notice in your table structure is that repetition of column.
E.g. team column not require in dps and mcs table, because from member column [connect to member table] you can easily find dps or mvs is under which team.
For your current problem you can use following query.
SELECT member.id,
member.membername,
Sum(dps.deposit) - Sum(dps.refund) AS balance,
Sum(mvs.mvs_total) AS mvs_balance,
Sum(loan.deposit) - Sum(loan.withdraw) AS balance,
Sum(loan.deposit2) - Sum(loan.withdraw2) AS balance2,
Sum(loan.total) - Sum(loan.lpay) AS outsstanding
FROM member AS member
LEFT JOIN dps AS dps
ON dps.member = member.id
LEFT JOIN mvs AS mvs
ON mvs.member = member.id
LEFT JOIN loan AS loan
ON loan.member = member.id
WHERE member.team = 1
AND mvs.ddate <= '2016-09-01'
-- AND loan.team = '10'
-- AND loan.cs = 'Core'
GROUP BY member.id

Related

How to manage SELECT data from 3 different table's using with JOIN?

I have 3 different table's.
driver
| id | name |
|-----|------|
| 10 | abc |
| 21 | xyz |
booking
| id | driver_id | booking_amount |
|----|-----------|----------------|
| 1 | 10 | 250 |
| 2 | 10 | 150 |
| 3 | 21 | 200 |
| 4 | 21 | 300 |
income
| id | driver_id | credit | debit | date |
|----|-----------|--------|-------|----------|
| 1 | 10 | 100 | | 1-1-2019 |
| 2 | 10 | 250 | | 2-1-2019 |
| 3 | 10 | | 200 | 3-1-2019 |
| 4 | 21 | 250 | | 1-1-2019 |
| 5 | 21 | 400 | | 2-1-2019 |
| 6 | 21 | | 50 | 3-1-2019 |
driver.id = booking.driver_id
driver.id = income.driver_id
I have use this query >>
SELECT driver.*, sum(booking.total_booking_income) as total_income
FROM driver
JOIN booking ON driver.id=booking.driver_id
GROUP BY driver.id
ORDER BY driver.id DESC
Note : but i am not able to add balance field in my this query.
i want to all driver records of income after group of booking and group of income by driver id like
| id | driver_id | driver_name | total_income | balance |
|----|-----------|-------------|--------------|---------|
| 1 | 10 | abc | 400 | -250 |
| 2 | 21 | xyz | 500 | 100 |
Assuming balance is the difference between the credit and the debit then:
SELECT d.*, sum(b.total_booking_income) as total_income,
i.balance
FROM driver d JOIN
booking b
ON d.id = b.driver_id LEFT JOIN
(SELECT i.driver_id,
SUM(i.credit) - SUM(i.debit) as balance
FROM income i
GROUP BY i.driver_id
) i
ON i.driver_id = d.id
GROUP BY d.id, i.balance
ORDER BY d.id DESC;

Php Two Table Update

I have a MySQL database that looks similar to this:
COUPON TABLE
| id | coupon_id | host | away |guess |skor |status|
+----+-----------+-----------+-------------+---------+------+------+
| 1 | 22 | Barcelona | Real Madrid | 1 |2-1 | 1 |
| 2 | 22 | Celtic | Porto | 2 |1-0 | 2 |
| 3 | 23 | Barcelona | Real Madrid | 1 |2-1 | 1 |
| 4 | 23 | M city | Chelsea | 1 |4-1 | 1 |
COUPONS TABLE
| id | user | Copuonstatus
+----+------------+-------------+
| 22 | Admin | 0 |
| 23 | Roberto | 0 |
I need to sort the results in a table that looks like this:
COUPONS TABLE
| id | user | Copuonstatus
+----+------------+-------------+
| 22 | Admin | 2 |
| 23 | Roberto | 1 |
NOTE :
Copuonstatus= default value 0
status = 0 waiting
status = 1 win
status = 2 los
im query try,
I could not
or update INNER JOIN use?
$q=$db->prepare("SELECT * FROM coupons INNER JOIN coupon ON
coupon.coupon_id = coupons.id AND coupon.status=2");
$q->execute();
$sss=$q->fetchAll(PDO::FETCH_GROUP);
if ( $sss > 0){
$db->exec("UPDATE coupons SET coupon_status = 2 FROM coupons ON
coupon.coupon_id = coupons.id WHERE coupons.Copuonstatus=0 ");}
UPDATE
coupons AS A
INNER JOIN coupon AS B ON A.id = B.coupon_id
SET
coupon_status = 2
WHERE
A.Copuonstatus = 0
Hope it help

how to avoid sub query or how can i save time

hi i am edited my question
there is 4 table
team
id | teamname |
1 | lal
2 | sobuj
member
id | membername| team |
1 | sagor| 1 |
2 | sumon| 1 |
dps
id | member| team | deposit | refund | ddate
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
and
mvs
id | member| team | mvs_total| refund | ddate
1 | 1 | 1 | 100 | 12 | 2016-09-01 |
2 | 2 | 1 | 120 | 34 | 2016-09-01 |
3 | 1 | 1 | 130 | 22 | 2016-09-01 |
4 | 3 | 1 | 120 | 0 | 2016-09-01 |
here is my query
$result=mysql_query(select * from member where team='1');
$row=mysql_fetch_array($result);
do{
$aresult=mysql_query(select sum(deposit)-sum(refund)as balance from dps where member='$row[id]');
$arow=mysql_fetch_array($aresult);
echo $arow[balance];
$bresult=mysql_query(select sum(mvs_total)as mvs_balance from mvs where member='$row[id]' and date1<='2016-09-01');
$brow=mysql_fetch_array($bresult);
}while($row=mysql_fetch_array($result));
its taking too much time
how to avoid sub query or how can i save time
advice plz
Thanks in advance
Try this,
select sum(deposit)-sum(refund) from dps JOIN member ON (dps. member = member.id) WHERE team='1';

SELECT records WHERE rows have difference on a specific column

I have a database table campaign_data. I need to select the customer_id where in the campaign there is difference in tariff. How can i do that with MySQL query. Here is some sample data.
SQL Fiddle Schema
| CAMPAIGN_ID | CUSTOMER_ID | CAMPAIGN_NAME | TARIFF |
---------------------------------------------------------
| 1 | 1 | Richmond | 100 |
| 2 | 1 | Sutton Coldfield | 75 |
| 3 | 1 | Putney | 100 |
| 4 | 1 | Kentish Town | 100 |
| 5 | 1 | Woking | 100 |
| 6 | 2 | Chiswick | 90 |
| 7 | 2 | Ealing | 100 |
| 8 | 2 | Camden | 100 |
| 9 | 3 | Croydon | 75 |
| 10 | 3 | Croydon1 | 100 |
| 11 | 3 | Archway | 100 |
| 12 | 4 | Ealing0 | 100 |
| 13 | 4 | Ealing01 | 100 |
| 14 | 4 | Ealing02 | 100 |
| 15 | 4 | Chingford | 100 |
| 16 | 4 | chingford01 | 100 |
Now as you can see customer id 1 , and 3 has different tariffs. I want to select them and leave the customer id 4 because it has campaigns with same tariffs.
Desired Output
| CUSTOMER_ID |
---------------
| 1 |
| 2 |
| 3 |
For clearification you can see customer 1 has 5 records. If in his 5 records the tariff is same (100) i want to avoid but if the tariff is not some as 4 records have 100 and one has 75, i want to select.
SELECT customer_id, count(DISTINCT tariff) as tariffs
FROM campaign_data
GROUP BY customer_id
HAVING tariffs > 1
you looking for this maybe
SELECT customer_id
FROM campaign_data
GROUP BY customer_id
HAVING count(DISTINCT tariff) > 1
http://sqlfiddle.com/#!2/48b6e/31
select
customer_id,
tariff
from campaign_data
group by customer_id
having sum(tariff)/count(tariff) <> tariff;

MySQL join - ordering results via another table PHP

I have 2 MySQL tables, one of which has a numeric column to orgainise the order I need the items to be displayed:
item_names
menu_id | dish_id | section_id | item_name
--------------------------------------------------
1 | 23 | 2 | Pie
1 | 24 | 2 | Fish
1 | 25 | 3 | Apples
1 | 26 | 2 | Onions
1 | 27 | 2 | Chips
link_extras
extra_id | dish_id | sort
-----------------------------
1 | 23 | 2
2 | 23 | 2
3 | 23 | 2
1 | 24 | 0
5 | 24 | 0
6 | 26 | 3
12 | 26 | 3
1 | 27 | 1
1 | 25 | 0
Basically what I am trying to do is extract each dish with a certain menu_id and section_id from the table item_names and order the output in respect to the sort column in the link_extras table.
so far:
$query="SELECT a.item_name, a.dish_id, b.sort
FROM item_names AS a, link_extras AS b
WHERE a.menu_id='1'
AND a.section_id='2'
AND b.dish_id=a.dish_id
GROUP BY b.dish_id
ORDER BY b.sort";
I am quite new to databases so would appreciate any help. The result I am after is
Fish
Chips
Pie
Onions
Unfortunately just can't get the order correct.
You need to use a simple JOIN
SELECT a.item_name, a.dish_id, b.sort
FROM item_names AS a
JOIN link_extras AS b
ON a.dish_id = b.dish_id
WHERE menu_id = 1
AND section_id = 2
GROUP BY b.dish_id
ORDER BY b.sort
Output:
| ITEM_NAME | DISH_ID | SORT |
------------------------------
| Fish | 24 | 0 |
| Chips | 27 | 1 |
| Pie | 23 | 2 |
| Onions | 26 | 3 |
See this SQLFiddle
SELECT
in.item_name
FROM item_names AS in
LEFT JOIN link_extras AS le
ON le.dish_id = in.dish_id
WHERE in.menu_id = 1
AND in.section_id = 2
ORDER BY le.sort
Demo Here
Output
| ITEM_NAME |
-------------
| Fish |
| Chips |
| Pie |
| Onions |

Categories