Join 4 tables with inner join - sql

I'm trying to write below query with INNER JOIN instead of AND
select ph.name, ph.position, d.name, COUNT(app.appointmentId)
from physician ph, works_in w, appointment app, department d
where ph.eid = w.physician
and d.did = w.department
and ph.eid = app.physician
group by ph.eid, ph.name, ph.position, d.name
.
I tried in this way, But It gets so many errors
select ph.name, ph.position, d.name, COUNT(app.appointmentId)
from physician ph inner join works_in w
on ph.eid = w.department,
department d inner join works_in w
on d.eid = w.department,
physician ph inner join appointment app
on ph.ph.eid = app.physician
group by ph.eid, ph.name, ph.position, d.name
How can I write it correctly with inner joins.

First let me reffer you to this site that explain all about join syntax's
Change your query to this:
select ph.name, ph.position, d.name, COUNT(app.appointmentId)
from physician ph
inner join works_in w
on ph.eid = w.department
inner join department d
on d.eid = w.department
inner join appointment app
on ph.ph.eid = app.physician
group by ph.eid, ph.name, ph.position, d.name
The syntax for joins is :
SELECT <COLUMNS>
FROM <Table>
INNER JOIN <Another_Table>
ON(<Relations>)
INNER JOIN <Another_table2>
ON(<Other Relations>

SELECT ph.name AS Name, ph.position AS Position,d.name AS Name, COUNT(app.appointmentId) AS Appointment
FROM physician AS ph INNER JOIN works_in AS w
ON ph.eid = w.department INNER JOIN department d
ON d.eid = w.department INNER JOIN appointment app ON ph.eid = app.physician
GROUP BY ph.eid,ph.name,ph.position,d.name

Related

sql, selecting using condition from another table

I have four tables,
game(game_id, game_name, date_of_release),
category(game_id, game_category),
company(company_id, company_name),
belongs_to(game_id, company_id)
I need to select game_id, game_name, and game_language where the company is 'Konami', I tried to do it like this
SELECT a.game_id, a.game_name, b.game_category, FROM game a
INNER JOIN category b ON a.game_id = b.game_id
INNER JOIN company c ON company_name = 'Konami'
INNER JOIN belongs_to d ON c.company_id = d.company_id
but it's selecting all the games regardless of the company name.
So what went wrong?
Your join is wrong.
SELECT
...
FROM
game a
INNER JOIN category b ON b.game_id = a.game_id
INNER JOIN belongs_to d ON d.game_id = a.game_id
INNER JOIN company c ON c.company_id = d.company_id
WHERE
c.company_name = 'Konami'
It seems that you not really understand joins. You should use them to connect tables, in you case
INNER JOIN company c ON company_name = 'Konami'
You connected row with info about Konami to all rows in previous tables. Try to think only about how you connect tables, not how you filter it while writing join clause
So, you can fix it like this: just add condition that shows how to connect tables
SELECT a.game_id, a.game_name, b.game_category
FROM game a
INNER JOIN category b ON a.game_id = b.game_id
INNER JOIN belongs_to d ON a.game_id = d.game_id
INNER JOIN company c ON c.company_id = d.company_id
WHERE company_name = 'Konami'
It's better to filter in where clause, but this will work fine too
SELECT a.game_id, a.game_name, b.game_category
FROM game a
INNER JOIN category b ON a.game_id = b.game_id
INNER JOIN belongs_to d ON a.game_id = d.game_id
INNER JOIN company c ON c.company_id = d.company_id
AND company_name = 'Konami'

Query table using inner join?

We are trying to find the names of the customers who have both a loan and an account in the same branch. Should we use inner join here? So far we have only written;
select DISTINCT customer.name
FROM Customer, Has_Loan, Branch, Has_Account
WHERE
We have tried a few different things without getting any further, so appreciate any kind of help or hint:)
You can try this :
SELECT DISTINCT Customer.Name
FROM Customer
INNER JOIN Has_Account ON Customer.Ssn = Has_Account.Assn
INNER JOIN Account ON Account.AccountNo = Has_Account.ANo
INNER JOIN Has_Loan ON Customer.Ssn = Has_Loan.Lssn
INNER JOIN Loan ON Loan.LoanNo = Has_Loan.LNo
WHERE Loan.BranchID = Account.BranchID
select c.name
from customer c
join has_loan hl on hl.lssn = c.ssn
join loan l on hl.lno = l.loanno
join has_account ha on ha.assn = c.ssn
join account a on ha.ano = a.accountno
join branch b on b.branchid = a.branchid
where l.branchid = a.branchid
group by c.name;

I want to retrieve records from 4 tables in mysql-Is there any alternative-this query is returning all records

SELECT * FROM enquiry e, sales_lead sl, customer c ,project p where ((e.customer_id=c.customer_id and e.project_id=p.project_id) or e.sales_lead_id=sl.sales_lead_id) and e.delete_status<>1
Use below JOIN query :
SELECT * FROM enquiry e
LEFT OUTER JOIN sales_lead sl ON e.sales_lead_id=sl.sales_lead_id
LEFT OUTER JOIN customer c ON e.customer_id=c.customer_id
LEFT OUTER JOIN project p ON e.project_id = p.project_id
WHERE e.delete_status <> 1

wm_concat returning multiple of the same for select statment

I have this select statement:
select m.title, m.category_code, c.company_name, wm_concat(d.director_name),
wm_concat(a.actor_name) as actors
from
actors a
inner join actor_in_movies aim
on aim.actor_id = a.actor_id
inner join movies m
on m.movie_id = aim.movie_id
inner join movie_directors md
on md.movie_id = m.movie_id
inner join directors d
on d.director_id = md.director_id
inner join companies c
on c.company_id = d.company_id
group by m.title, m.category_code, c.company_name;
and it returns the same directors name one time for every actor....any way around this happening?
Tried this too:
select m.title, cat.description, c.company_name, wm_concat(d.director_name),
wm_concat(a.actor_name) as actors
from
movie_categories cat
inner join movies m
on m.category_code = cat.category_code
inner join movie_directors md
on md.movie_id = m.movie_id
inner join directors d
on d.director_id = md.director_id
inner join companies c
on c.company_id = d.company_id
inner join actor_in_movies aim
on aim.movie_id = m.movie_id
inner join actors a
on a.actor_id = aim.actor_id
group by m.title, cat.description, c.company_name;
with the same results
EDIT:
Guess I just needed to think more, used distinct and got it!
select m.title, cat.description, c.company_name, wm_concat(distinct
d.director_name),
wm_concat(a.actor_name) as actors
from
movie_categories cat
inner join movies m
on m.category_code = cat.category_code
inner join movie_directors md
on md.movie_id = m.movie_id
inner join directors d
on d.director_id = md.director_id
inner join companies c
on c.company_id = d.company_id
inner join actor_in_movies aim
on aim.movie_id = m.movie_id
inner join actors a
on a.actor_id = aim.actor_id
group by m.title, cat.description, c.company_name;

Trouble with Full Text Search query

I'm having trouble with this full text search query I'm trying to run. I need to do a full text search on two tables. If any of the terms are in either table I need to return the records from the first table.
select R.* from Request R
inner join Patients P on R.PatientID = P.PatientID
inner join containstable(Request,(*),#keywords)AS KEY_TBL
ON R.RequestID = KEY_TBL.[Key]
full outer join
(select R.* from Request R
inner join Patients P on R.PatientID = P.PatientID
inner join containstable(Patients,(*),#keywords) AS KEY_TBL2
ON P.PatientID = KEY_TBL2.[Key]) as b on R.RequestID = b.RequestID
All I needed was a Union instead of a full outer join.
select R.* from Request R
inner join Patients P on R.PatientID = P.PatientID
inner join containstable(Request,(*),#keywords)AS KEY_TBL
ON R.RequestID = KEY_TBL.[Key]
UNION
select R.* from Request R
inner join Patients P on R.PatientID = P.PatientID
inner join containstable(Patients,(*),#keywords) AS KEY_TBL2
ON P.PatientID = KEY_TBL2.[Key]