How to perform these joins with MySQL? - mysql

Having these tables:
clients
-------------------
id
name
town
companies
------------------
id
name
credits
------------------
clients_id
companies_id
credit
Example:
clients
-------------
1 john doe London
2 jane smith Paris
companies
-------------
1 mycompany1
2 mycompany2
credits
-------------
1 1 5000
1 2 3250
2 2 4500
How can I list all the clients including an additional column for each credit she has in a company? I also need to alias each of these columns in this fashion: "credit_"
Something like:
id name town credit_1 credit_2
1 john doe London 5000 3250
2 jane smith Paris NULL 4500
I'm not sure if doing two LEFT JOINS to credits would work.

Related

Complex mysql query counting

I have 1 table name "companies" with several datas like :
Id
Owner
Company
Job
1
John Doe
Company 1
CEO
1
John Doe
Company 2
CEO
1
John Doe
Company 3
CEO
1
Gab Durand
Company 4
CEO
1
Rob Dujn
Company 5
CTO
1
Alex DoeMorse
Company 6
COO
What I need is to get 1 line by company with a row calculating the number of company own by each person.
This is my desired output :
Id
Owner
Company
Job
Count
1
John Doe
Company 1
CEO
3
1
John Doe
Company 2
CEO
3
1
John Doe
Company 3
CEO
3
1
Gab Durand
Company 4
CEO
1
1
Rob Dujn
Company 5
CTO
1
1
Alex DoeMorse
Company 6
COO
1
What could be the mysql query?
EDIT : DB version 5.6.51
Thanks!
You can add an extra column containing analytic function such as
COUNT(*) OVER (PARTITION BY Id, owner) AS count
if DB version is 8.0
As having a former DB version, prefer using correlated subquery such as
SELECT Id, Owner, company, Job,
(SELECT COUNT(*)
FROM t
WHERE id = tt.id
AND Owner = tt.Owner ) AS count
FROM t AS tt
Demo
Query:
SELECT Owner, Job, count(Company) NoOfCompanies
FROM companies
WHERE Job='CEO'
GROUP BY Owner,Job;
Note: Grouping done on Owner and Job expecting you have other values along with 'CEO'

I am trying to return combined rows in the same table based on a key in a second table

I probably haven't explained this very well in the title but I have two tables. Here is a simple version.
channel_data
entry_id channel_id first_name last_name model other_fields
1 4 John Smith
2 4 Jane Doe
3 4 Bill Evans
4 15 235
5 15 765
6 15 543
7 15 723
8 15 354
9 15 976
10 1 xxx
11 2 yyy
12 3 123
channel_titles
entry_id author_id channel_id
1 101 4
2 102 4
3 103 4
4 101 15
5 101 15
6 101 15
7 102 15
8 102 15
9 103 15
10 101 1
11 102 2
12 103 3
I am not able to re-model the data unfortunately.
I need to list all the rows with a channel_id 15 from channel_data and beside them the first_name and last_name which has the same author_id from channel_titles.
What I want to return is this:
Model First Name Last Name
---------------------------------
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
If Model was in one table and Names were in another this would be much simpler but I'm not sure how to go about this when they are in the same table.
========================================
Edited to clarify.
I need to get each model with a channel_id 15 from channel_data
For each model I need to look up the entry_id in channel_titles to find the author_id
I need to find the row with that author_id AND channel_id 4 in channel titles (each row with channel_id 4 has a unique author_id).
I need to take the entry_id of this row back to channel_data and get the first_name and last_name to go with the model.
I am well aware that the data is not structured well but that is what I have to work with. I am trying to accomplish a very small task in a much larger system, remodelling the data is not an option at this point.
I think sub-queries might be what I am looking for but this is not my area at all usually.
Ok, that is convoluted. However, based on your description, this query should give you the results you want. The WHERE and JOIN descriptions follow the logic you have described in your question.
SELECT cd1.model, cd2.first_name, cd2.last_name
FROM channel_data cd1
JOIN channel_titles ct1 ON ct1.entry_id = cd1.entry_id
JOIN channel_titles ct2 ON ct2.channel_id = 4 AND ct2.author_id = ct1.author_id
JOIN channel_data cd2 ON cd2.entry_id = ct2.entry_id
WHERE cd1.channel_id = 15
ORDER BY cd1.entry_id
Output:
model first_name last_name
235 John Smith
765 John Smith
543 John Smith
723 Jane Doe
354 Jane Doe
976 Bill Evans
Demo on SQLFiddle

Join a report table based on three relational tables

Below are the given tables:
`student` `subject`
------------------- -------------------
id name id subject
------------------- -------------------
1 Alice 1 Maths
2 Bob 2 Science
3 Eve 3 Economics
------------------- -------------------
`marks`
-----------------------------------------------------
id student_id subject_id marks
-----------------------------------------------------
1 1 1 30
2 1 2 40
3 2 3 50
4 3 1 60
5 3 2 70
-----------------------------------------------------
I need an output which should look like below:
`o/p`
----------------------------------------------
name subject marks
----------------------------------------------
Alice Maths 30
Alice Science 10
Alice Economics NULL
Bob Maths NULL
Bob Science NULL
Bob Economics 50
Eve Maths 60
Eve Science 70
Eve Economics NULL
----------------------------------------------
Please note that I am targeting MySQL 5.6.x syntax and I have created a SQL fiddle of above here to ease access to the question.
Here you go
select
st.name,
su.subject,
m.marks
from student as st
cross join subject as su
left join marks as m on m.student_id=st.id and m.subject_id=su.id
order by st.name, su.subject
SELECT student.name,
subject.subject,
marks.marks
FROM student
JOIN subject ON student.id = subject.id
JOIN marks ON student.id = marks.id
ORDER BY student.name,
subject.subject

get members count with family detail and hof detail mysql

persons table
-----------------
id name fatherid motherid famid
---------------------
1 ras lovely beautiful 5
2 abc def ghi 5
3 xyz abc aay 4
family table
----------------
famid hof ( id from persons)
------------------------
4 3
5 2
output required
-------------------
membercount(id with famid) hof_name hof_id(id from person) famid
-------------------
2 abc 2 5
1 xyz 3 4
please help me to get the correct query

prediction on rowwise data or progressive data

I am working on employee attrition analysis with a table having rowwise data for a (employee like Id, name, Date_Join Date_Relieving Dept Role etc)
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11 (ie she is available)
I can run regression on this data to find the beta coefficient
eID eName Joining Releiving Dept Married Experience
123 John Doe 10Oct15 12Oct16 HR No 12
234 Jen Doee 01jan16 -NA- HR No 11
But I've seen other approach too.. where employee have multiple entries depending on their difference between joining date and current month or relieving month(say Employee A joined in Jan and Left in Dec so he'll have 12 entries updating corresponding columns like experience and marriage etc)
eID eName Dept Married Experience
123 John Doe HR No 0
123 John Doe HR No 1
123 John Doe HR Yes 2
123 John Doe HR Yes 3
can someone tell what differentiate two approaches.. and what is the outcome of this second approach.