sum is not getting correctly while joining the tables - php

Here i have 3 tables name A,B,C respectively and i want to join all the tables and fetch out the results
In order to get the desired output i wrote my code like this
SELECT A.date as d_date,B.agent_name,
(SELECT SUM(B.profit) FROM B WHERE A.id = B.bill_id) AS total_profit,
SUM(C.total_price) AS t_price,SUM(C.total_dc) AS t_dc
FROM A LEFT JOIN B ON A.id=B.bill_id
LEFT JOIN C ON C.data_id=B.id
WHERE DATE(A.date) BETWEEN '{$start_date}' AND '{$end_date}'
AND A.customerid=406
GROUP BY Date(A.date),A.customerid
ORDER BY A.id;
The problem is am getting Purchase value as the first value of the profit column from the table B.
i want my desired output to be like this
Name Date Purchase t_price t_dc
Ned 2019-07-26 210.60 80 40
but am getting like this
Name Date Purchase t_price t_dc
Ned 2019-07-26 15.60 80 40
here is the demo http://sqlfiddle.com/#!9/c85a910/3

The problem here is, Table C have 2 rows and both having data_id as 67159. So when you will join it with table B it will count the profit for bill_id 67159 twice. You have to use one condition to pick only 1 row. I have updated your query to -
SELECT A.date as d_date,B.agent_name, SUM(B.profit) AS total_profit,
SUM(C.total_price) AS t_price,SUM(C.total_dc) AS t_dc
FROM A LEFT JOIN B ON A.id=B.bill_id
AND A.customerid = B.user_id
LEFT JOIN C ON C.data_id=B.id
WHERE DATE(A.date) BETWEEN '2019-07-26' AND '2019-07-26'
AND A.customerid=406
GROUP BY Date(A.date),B.agent_name
ORDER BY A.id;
This query is giving total_profit as 226.2.

Related

Select where joined table has the more listed values

I have 3 tables :
A: id, name
B: id, name
C: id_A, id_B
The relation is A has many B.
There is another table with the relation has many B.
What I would like to do is find the A that has the most B.id I listed.
Something like
SELECT * FROM A JOIN C ON A.id=C.id_A JOIN B ON B.id=C.id_B WHERE B.ID HAVE_MOST(5,22,39,110,235);
It will return the first A that have all the values and if it's not found, it will return the first A that have a combination of 4 of those values, etc.
Is this possible in mysql ?
This seems to do the job :
SELECT *, count(A.id) as number FROM A JOIN C ON A.id=C.id_A JOIN B ON B.id=C.id_B WHERE B.ID IN (5,22,39,110,235) GROUP BY A.id ORDER BY number DESC LIMIT 1

Mysql left join returns more than one row

I have 3 tables and some fields' names are the same. Here is the first table named semp:
The second one's name semp_k:
And the third is semp_y:
You see, the main table is the first and the others are related it. The first table has got 3 row. So when I fetch it, it must return 3 row. But when I fetch the first table, it multiples returned rows with sum of second and third table. Here is my code:
SELECT s.*, k.*, y.* FROM semp AS s LEFT JOIN semp_k AS k ON s.no = k.semp_no LEFT JOIN semp_y AS y ON s.no = y.semp_no WHERE s.durum = 1 ORDER BY s.bas_t DESC
use MySQL group by
group by s.no
or try this :-
SELECT
s.*, k.*, y.*
FROM
semp AS s
LEFT JOIN semp_k AS k ON s. NO = k.semp_no
LEFT JOIN semp_y AS y ON s. NO = y.semp_no
WHERE
s.durum = 1
GROUP BY s.no
ORDER BY
s.bas_t DESC
You need to use group by.
Your query should be like this ;
SELECT
s.*, k.*, y.*
FROM
semp AS s
LEFT JOIN semp_k AS k ON s. NO = k.semp_no
LEFT JOIN semp_y AS y ON s. NO = y.semp_no
WHERE
s.durum = 1
GROUP BY s.no
ORDER BY
s.bas_t DESC

Mysql Query Not Working giving duplicate records

I have two table tblclients and mod_autorenewdomain
tblclients columns which I required:
id
firstname
lastname
mod_autorenewdomain columns:
id
userid
autorenew
days
I have inserted in mod_autorenewel table, next I want to show the clients which are not added in mod_autorenewel table
The query I am trying is not working
Select c.id,c.firstname,c.lastname from tblclients as c join mod_autorenewdomain as m ON c.id!=m.userid ORDER BY c.id ASC
It is showing the result as follows:
id firstname lastname
12 Adil Mukarram
13 M. Mahad Ashraf
14 Zeeshan Mushtaq
14 Zeeshan Mushtaq
15 Muhammad Suhaib
15 Muhammad Suhaib
Firstly I have added with id 12 and 13 in mod_autorenewdomain table so these records does not show but they are displaying
Secondly the other records are repeating Kindly guide me.
You want to use a left join to find non-matching records:
Select c.id, c.firstname, c.lastname
from tblclients c left join
mod_autorenewdomain m
on c.id = m.userid
where m.userid is null
order by c.id ASC;
That is, use the FROM clause to look for matches. Then use the WHERE clause to find the records that do not match.

I cant select data from database with inner join using mysql

I cant select data from database .
My table structrure is given below
customer table
id name
10 geetha
customer country table
id cust_id country
1 10 6
2 10 16
I got the result like these way
customer name country
geetha 6
geetha 16
But i want to get the one customer data only one time ie with out repeating.
customer name country
geetha 6
my query is
SELECT customer.name,customer.id,customer_country.country_id, customer_country.cust_id
FROM customer
INNER JOIN customer_country on customer.id = customer_country.cust_id
If you have duplicates in the customer_country table, then you need to choose one of them. Here is one method using max():
select c.name, max(cc.country_id)
from customer c inner join
customer_country cc
on c.id = cc.cust_id
group by c.name;
If you want all of them in a list, use group_concat():
select c.name, group_concat(cc.country_id) as countries
from customer c inner join
customer_country cc
on c.id = cc.cust_id
group by c.name;
For first record only, apply to the end: limit 1
SELECT customer.name,customer.id,customer_country.country_id,
customer_country.cust_id from customer
inner join customer_country on customer.id= customer_country.cust_id limit 1
try this i add distinct before customer_country.cust_id
SELECT customer.name,customer.id,customer_country.country_id, distinct customer_country.cust_id
FROM customer
INNER JOIN customer_country on customer.id = customer_country.cust_id

How to query for One - Many Relation

I have two database tables with "one to many" relationship
let say, table A and Table B
One field from Table A is a foreign key in table B
I wanna fetch just one record from table A as well as table B (given table A's primary key)
table A
id name
--------------------
1 ABC
2 XYZ
Table B
id A_id email
------------------------------------------------
1 1 temp#temp1.com
2 1 temp#temp2.com
3 1 temp#temp3.com
4 2 temp#temp4.com
4 2 temp#temp5.com
Answer should be like this (Single Record From Table B)
For a.id = 1
A.id, A.name,B.email
-------------------------
1, ABC, temp#temp1.com
For a.id = 2
A.id, A.name,B.email
-------------------------
1, XYZ, temp#temp4.com
I used this query, but it returns all the records from table B(as table B has multiple records for each record in Table A)
SELECT a.id,a.name, b.email FROM A a, B b WHERE a.id = 1 AND b.A_id = a.id
I have created a SQL Fiddle for the demo here: http://www.sqlfiddle.com/#!2/15ae7/5
The query that you can use for getting the desired output is:
SELECT
tableA.id,
tableA.name,
tableB.email
FROM tableA
LEFT OUTER JOIN tableB ON tableB.A_id = tableA.id
GROUP BY tableB.A_id;
For more information on JOINS and GROUP BY you can refer to the following pages:
http://dev.mysql.com/doc/refman/5.0/en/join.html
https://dev.mysql.com/doc/refman/5.0/en/group-by-functions.html
Try a JOIN
SELECT
a.id,
a.name,
b.email
FROM A a
LEFT JOIN B b ON b.A_id = a.id
WHERE a.id = 1;
GROUP BY b.A_id
More info on JOINS
You can use LIMIT if you want only one record.
SELECT a.id,a.name, b.email FROM A a, B b WHERE a.id = 1 AND b.A_id = a.id LIMIT 0,1
Hope this helps you.

Categories