mysql join count lines in third table - mysql

I have three mySQL-tables like this:
client network activities
\_id \_id \_id
\_name \_name \_client_id
\_[...] \[...] \_[...]
\_network_id
Now I want to have the following result:
client.*, network.name, number_of_activities_for_each_client
Actually I have this select-statement:
select client.*, network.name
from client
left join network
on client.network_id = network.id
How do I extend the statement? I would also like to see all clients.

SELECT c.*, n.name, COUNT(a.id) number_of_activities_for_each_client
FROM clients c
LEFT JOIN network n
ON n.id = c.network_id
LEFT JOIN activities a
ON a.client_id = c.id
GROUP BY c.id

Related

Join of three different mysql queries tables

I want to join three different queries.
Here are my queries
Query1:
SELECT u.user_name,u.first_name
FROM users u join users_cstm uc on u.id=uc.id_c
WHERE u.deleted=0?
Query2:
SELECT l.assigned_user_id,count(*) AS lead_count
FROM lead l GROUP BY l.assigned_user_id?
Query3:
SELECT l.assigned_user_id,AVG(DATEDIFF(l.date_modified,l.date_entered)) AS avgdays
FROM leads l GROUP BY l.assigned_user_id?
and so on.
This is the result I am looking for user_name, first_name,lead_count,avgdays. from three tables.
BigQuery join of three tables
I have tried my solution from the above link. But I didn't get the result.
Try below query:-
SELECT u.user_name,u.first_name,
lead_table.lead_count,lead_table.avgdays
FROM users u join users_cstm uc on u.id=uc.id_c
left join
(SELECT l.assigned_user_id,
AVG(l.date_modified) AS avgdays,
count(*) AS lead_count
FROM leads l GROUP BY l.assigned_user_id) lead_table
on u.id=lead_table.assigned_user_id
WHERE u.deleted=0;
Try this:
SELECT id,user_name,first_name,lead_count,avgdays from
(SELECT id,user_name,first_name,lead_count from
(SELECT u.id,u.user_name,u.first_name FROM users u,users_cstm uc where u.id=uc.id_c and u.deleted=0) as a
LEFT JOIN
(SELECT l.assigned_user_id,count(*) AS lead_count FROM lead l GROUP BY l.assigned_user_id) as b
on a.id = b.assigned_user_id) as a
LEFT JOIN
(SELECT l.assigned_user_id,AVG(DATEDIFF(l.date_modified,l.date_entered)) AS avgdays
FROM leads l GROUP BY l.assigned_user_id) as b
on a.id = b.assigned_user_id

LEFT JOIN 2 INNER JOIN Tables Without Subquery

I have 3 Tables: Jobs, Users, Applications.
I'd like a list of all jobs and optionally "any" applications submitted per each job but only if I have data for the user who submitted the application.
I vaguely remember seeing some obscure syntax in which 2 tables are joined to a query as if being a single table, something like:
select j.title, a.id, u.name from jobs j
left join applications a join users u on a.job_id=j.id and a.user_id = u.id
I know I could accomplish this with a subquery, but does a syntax of this nature exist?
Update
The answers I've received so far assume I want only a basic join. Though they break the intended logic posed in the question. Perhaps I've should've posted the subquery equivalent to illustrate the type of query I'd like to run.
select j.job_id, au.application_id, au.user_id from jobs j
left join (
select a.job_id, u.user_id from applications a
join users u on u.user_id = a.user_id
) au on j.job_id = au.job_id
You need both of these to be outer joins if you chain them the "simple" way:
select *
from jobs j
left outer join application a on a.job_id = j.id
left outer join users u on u.user_id = a.id
Since I take it that an application requires a user you can force the inner join to happen first in this way. Maybe that's the syntax question you were asking about. The parens are generally optional in my experience but I don't know a lot of MySQL:
select *
from jobs j
left outer join (application a
inner join users u on a.user_id = u.id) on a.job_id = j.id
This is where a right join comes up sometimes so the query can be written from top to bottom without any nesting:
select *
from application a
inner join users u on u.user_id = a.id on
right outer join jobs j on a.job_id = j.id
You were almost there, your only mistake was to put the both join conditions after the second join.
select *
from jobs j
left join application a on on.job_id = j.id
left join users u on u.user_id = u.id
Note that the maximum number of tables that can be referenced in a join is 61 (in MySQL and probably most of the popular DBMS).
See documentation.

Data join multiple tables in MySQL

I'm really really a newbie to this so I hope i can explain myself with what I am having trouble with.
I have several tables that i need to extract data from into 1 table. So far I am able to extract from 2 tables but not from 3 or more.
This is what I have from extracting from 2 tables:
select C.id , C.business, AP.firstname, AP.lastname from claims C JOIN affected_people AP ON C.ID = AP.claimid
I have another table name named 'Messages' which I need to extract 'comments and dateread'.
This table relates to the others via 'claimid'.
How do I extract from all three into 1 table?
Help please.
D_Klutz
I did not contemplate an issue I am having with the results obtained. Turns out that there are mutliple messages sent out per claimid but we are looking for is the very last message. How can it be coded to select only the very last message? All messages sent have a timestamp on it. Thanks for your help
Adding another join on Messages should work:
select
C.id ,
C.business,
AP.firstname,
AP.lastname,
M.comments,
M.dateread
from
claims C
JOIN
affected_people AP
ON C.ID = AP.claimid
join
Messages M
on M.claimid = C.ID
Your query should be like this.
select
C.id ,
C.business,
AP.firstname,
AP.lastname,
m.comments,
m. dateread
from
claims C
JOIN
affected_people AP ON C.ID = AP.claimid
JOIN
Messages M on C.ID=M.claimid
Try it
select C.ID, C.business, AP.firstname, AP.lastname, M.Message from claims AS C INNER JOIN affected_people as AP ON C.Id = AP.claimid inner join Messages as M on C.ID = M.claimid

Cartesian join with multple outer joins to common root

I have the following schema.
I can run two queries fairly simply
select * from booking_model_assignment
join booking_model on booking_model_assignment.booking_model_id = booking_model.id
left outer join axis_channel_mappings on bmi_id = axis_channel_mappings.assignment_id
left outer join axis_revenue_stream_mappings on bmi_id = axis_revenue_stream_mappings.assignment_id
which will give me all of the combinations of channel mappings and 'revenue_stream_mappings' which fit a booking model, with Null if there is one which only matches in one of the tables.
The other query
select * from axis_channel join axis_revenue_stream
Gives all of the possible combinations of channels and revenue streams.
What I would like is a query which will give all of the combinations, and the booking_model if that combination matches.
Any time I try to join or subquery I seem to get too many, or too few results. I think the issue is that I want the assignment_id to match across outer joins but only if there is an outer join.
The schema is laid out like this so it will be possible to add new axis and fit models to combinations, so if there is an easier way to achieve this I would be open to changing the schema.
EDIT
I have a partial solution based on Eggyal's answer but it is not extendable.
SELECT c.*, r.*, GROUP_CONCAT(a.bmi_id), GROUP_CONCAT(b.name) AS booking_models
FROM axis_channel c
CROSS JOIN axis_revenue_stream r
LEFT JOIN axis_channel_mappings cm ON cm.channel_id = c.id
LEFT JOIN axis_revenue_stream_mappings rm ON rm.revenue_stream_id = r.id
LEFT JOIN booking_model_assignment a ON (a.bmi_id = cm.assignment_id
AND a.bmi_id = rm.assignment_id)
OR (a.bmi_id = cm.assignment_id
AND rm.assignment_id IS NULL)
OR (cm.assignment_id IS NULL
AND a.bmi_id = cm.assignment_id)
LEFT JOIN booking_model b ON b.id = a.booking_model_id
GROUP BY c.id, r.id
But if I were to add more axes this query would grow way to cumbersome.
SELECT c.*, r.*, GROUP_CONCAT(b.name) AS booking_models
FROM axis_channel c
CROSS JOIN axis_revenue_stream r
LEFT JOIN axis_channel_mappings cm ON cm.channel_id = c.id
LEFT JOIN axis_revenue_stream_mappings rm ON rm.revenue_stream_id = r.id
LEFT JOIN booking_model_assignment a ON a.bmi_id = cm.assignment_id
AND a.bmi_id = rm.assignment_id
LEFT JOIN booking_model b ON b.id = a.booking_model_id
GROUP BY c.id, r.id

Complex MySQL query with multiple select statements

I have three tables in Mysql that are link together:
Profile (ID, Name, Stuff..)
Contact(ID, ProfileID,desc,Ord)
Address(ID,ProfileID, desc, Ord)
Now I need to select all profile from the profile table, with the “desc” field from Contact and Address where Ord = 1. (this is for a search function where in a table I’ll display the name, main contact info and main Address of a client.
I can currently do this with three separate SQL request:
SELECT Name, ID FROM Profile WHERE name=”bla”
Then in a foreach loop, I’ll run the other two requests:
SELECT ProfileID, desc FROM Contact WHERE ProfileID=MyProfileID AND Ord=1
SELECT ProfileID, desc FROM Address WHERE ProfileID=MyProfileID AND Ord=1
I know you can do multiple SELECT in one query, is there a way I could group all three SELECT into one query?
You should be able to JOIN the tables on the profile.id and the profileid in the other tables.
If you are sure the profileid exists in all three tables, then you can use an INNER JOIN. The INNER JOIN returns matching rows in all of the tables:
select p.id,
p.name,
c.desc ContactDesc,
a.desc AddressDesc
from profile p
inner join contact c
on p.id = c.profileid
inner join address a
on p.id = a.profileid
where p.name = 'bla'
and c.ord = 1
and a.ord = 1
If you are not sure that you will have matching rows, then you can use a LEFT JOIN:
select p.id,
p.name,
c.desc ContactDesc,
a.desc AddressDesc
from profile p
left join contact c
on p.id = c.profileid
and c.ord = 1
left join address a
on p.id = a.profileid
and a.ord = 1
where p.name = 'bla'
If you need help learning JOIN syntax, here is a great visual explanation of joins
This query below only selects column when an ID from Profile table has atleast one match on tables: Contact and Address. If one or both of them are nullable, use LEFT JOIN instead of INNER JOIN because LEFT JOIN displays all records from the Left-hand side table regardless if it has a match on other tables or not.
SELECT a.*,
b.desc as BDESC,
c.desc as CDESC
FROM Profile a
INNER JOIN Contact b
ON a.ID = b.ProfileID
INNER JOIN Address c
ON a.ID = c.ProfileID
WHERE b.ORD = 1 AND
c.ORD = 1 AND
a.Name = 'nameHERE'
The LEFT JOIN version:
SELECT a.*,
b.desc as BDESC,
c.desc as CDESC
FROM Profile a
INNER JOIN Contact b
ON a.ID = b.ProfileID AND b.ORD = 1
INNER JOIN Address c
ON a.ID = c.ProfileID AND c.ORD = 1
WHERE a.Name = 'nameHERE'
To further gain more knowledge about joins, kindly visit the link below:
Visual Representation of SQL Joins
i have created working demo as your requirement :
The query bellow will retrieve all matching records from the database.its retrieving profile id,name stufff and description of contact tables
select p.id,p.name,p.stauff,c.descr,a.descr from profile as p
inner join contact as c on c.profileid=p.id
inner join address as a on a.profileid=p.id
where p.name="bla" and c.ord=1 and a.ord=1