Mysql Query Not Working giving duplicate records - php

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.

Related

Sum values from a SQL table to a value from other SQL table

I have a PHP page like this:
ID
Name
Hours Flown
1
Joao
7
2
Andre
10
3
Tiago
15
And I want that "Hours Flown" column show a value from one column from a SQL table (users) with a sum of another SQL table (flights) where ID matches the "users" table.
I have a while($row = $result_list_pilots->fetch_assoc()) to show the table if that helps...
SQL query for now is a bit simple "SELECT * FROM users ORDER BY name;". Already tried some JOIN examples but without success as the imported_hours gets duplicated as result from flights table is being found.
Tables:
[Tables]
How to do this?
I think here is what you want :
SELECT
u.ID
, u.Name
, SUM(ISNULL(f.HoursFlown,0)) + MAX(Imported_hours) 'Hours Flown'
FROM Users u
LEFT JOIN Flights f
ON u.ID = f.User_id -- or whatever the fk is
GROUP BY u.ID, u.Name
SELECT u.ID, u.Name, SUM(f.HoursFlown)
FROM Users u
INNER JOIN Flights f ON u.Name = f.Name
GROUP BY u.ID, u.Name

sum is not getting correctly while joining the tables

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.

MySQL Multi-Join Query

I have multiple tables I am trying to grab data from in a single query. I seem to be close to a solution but can not seem to get the data result I am expecting.
Examples of my tables are as follows (fields have been truncated):
Table c
id
name
abbreviation
Table mr (relationship table tat ties tables c and m together by ID)
id
c.id
m.id
Table m
id
Table cnt
id
c.id
Table cmp
id
cnt.id
active
What I WANT is all fields from C, all fields from M where m.id = c.id, all active (active = 1) id's from CMP that match on cnt.id.
My most recent query (after dozens of iterations) is:
SELECT c.id AS id
, c.name AS name
, c.abbreviation AS abbr
, c.active AS active
, c.last_modified AS last_modified
, c.modified_by AS modified_by
, mr.media_id
, mr.related_object_table
, mr.related_object_id
, m.orig_name AS img_name
, m.unique_name AS img_slug
, m.file_type AS confed_file_type
, m.file_size AS file_size
, COUNT('cmp.id') AS comps
FROM confederations AS c
LEFT JOIN media_relationships AS mr
ON mr.related_object_id = c.id
AND mr.related_object_table = 'confederations'
LEFT JOIN media AS m
ON m.id = mr.media_id
INNER JOIN countries AS cnt
ON cnt.confederations_id = c.id
INNER JOIN competitions AS cmp
ON cmp.countries_id = cnt.id
AND cmp.active = 1;
I am not proficient with Joins.
Basically, the result i am expecting is: For each Confederation (table C) I want that confederations name, abbreviation, active status (active), last modified date, modified by; from the Media Relationship table (table MR) I want the image id associated with that confederation so I can use that id to grab the image name and image slug for the confederations primary image from the Media table (M).
Now I also want the total number of Competitions (table CMP) for a given Confederation. Competitions are stored with a Country ID that is tied to the primary key ID of a country in the Countries Table (table CNT). Each Country in table CNT has a Confederations ID. So to get the total number of Competitions per Confederation I am 'trying' to get all Countries within their respective Confederation by CONFEDERATIONS_ID in table CNT, then foreach confederation I want select all the competitions from table CMP with matching COUNTRIES_ID from the group of country id's for that given confederation. (At this point i think i am confusing myself with how to get what i want)
Somehow I am getting the CORRECT NUMBER of competitions, but I am getting duplicate Confederations as results. For Example I am getting something similar to this (assume I have 3 different confederations with 2, 1, and 3 competitions respectively):
Competitions 1 : name 1 | abbreviation 1 | image 1 | total competitions = 2;
Competitions 1 : name 1 | abbreviation 1 | image 1 | total competitions = 1;
Competitions 1 : name 1 | abbreviation 1 | image 1 | total competitions = 3
What am i doing wrong?
Through trial and error, I actually solved this on my own. I came back to post my answer and see Degan's answer, and though it is written differently than mine I think its very close to what I ended up with:
SELECT
cnf.id AS confed_id, cnf.name AS confed_name, cnf.abbreviation AS
confed_abbr, cnf.active AS confed_active, cnf.modified_by AS
confed_mod_by, cnf.last_modified AS confed_last_mod,
COUNT(cnt.id) AS total_countries,
COUNT(cmp.id) AS total_comps,
mr.media_id, mr.related_object_table, mr.related_object_id,
mr.primary_img,
m.orig_name AS img_name, m.unique_name AS img_slug, m.file_type AS file_type
FROM confederations AS cnf
LEFT JOIN media_relationships AS mr
ON mr.related_object_id = cnf.id AND mr.related_object_table = 'confederations'
LEFT JOIN media AS m
ON m.id = mr.media_id
LEFT JOIN countries AS cnt
ON cnt.confederations_id = cnf.id AND cnt.active = 1
LEFT JOIN competitions AS cmp
ON cmp.countries_id = cnt.id AND cmp.active = 1
GROUP BY cnf.id
So farthis seems to be giving me results i can use. I am not certain if my choice of Left Join for all my joins is in fact giving me everything i need (it SEEMS to be) and whether this will omit/add records once the tables get larger. If anyone can point out a problem in my query and my choice of using Left Join as opposed to a combination of LEFT and INNER JOINs as Degan did, that would be helpful.
When aggregating you need to group by.
Perhaps this is close to what you are looking for:
SELECT c.id AS id
, c.name AS name
, c.abbreviation AS abbr
, m.orig_name AS img_name
, SUM('cmp.id') AS comps
FROM confederations AS c
LEFT JOIN media_relationships AS mr
ON mr.related_object_id = c.id
AND mr.related_object_table = 'confederations'
LEFT JOIN media AS m
ON m.id = mr.media_id
INNER JOIN countries AS cnt
ON cnt.confederations_id = c.id
INNER JOIN competitions AS cmp
ON cmp.countries_id = cnt.id
AND cmp.active = 1
GROUP BY c.id AS id
, c.name AS name
, c.abbreviation AS abbr
, m.orig_name AS img_name

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

Mysql left-join, count by unique value in a field not working

I have a table like below.
I want to be able to get a count of group_id then group by group_id then left join groups table where groups_user.group_id = groups.id but I'm only getting one result back.
I want group by unique group_id then count of each duplicate group_id. So far my query is like below:
SELECT 'groups.group' ,COUNT('groups_users.group_id') as groups_count
FROM `groups_users`
LEFT JOIN groups
ON 'groups_users.group_id' = 'groups.id'
GROUP BY 'groups_users.group_id'
Table:
id group_id user_id
26 3 1
22 2 1
19 1 1
20 1 2
21 1 4
Where am I getting wrong here?
I think you've got the JOIN backwards. Try:
SELECT g.group, COUNT(gu.group_id) AS groups_count
FROM groups g
LEFT JOIN groups_users gu
ON g.id = gu.group_id
GROUP BY g.group;

Categories