Joining two tables and linking the third table - mysql

I want to do a left join on the following two tables and link it with another table
for eg.
I have a table called students
SID (pk)
fname
lname
assignment table
aid (pk)
dur
SID (fk)
pid (fk)
professor table
pid (pk)
pname
I will take fname from first table and pid from second table left join them and display corresponding pname from professor table.
So i want the table to look like this later
Fname Pname
Raju Jack
RAm Null
jim john
Thanks
I have written the code like this but it is not working
select students.fname, professor.pname
from student
LEFT JOIN professor ON professor.pid = assignment.pid
i want to do left join so i will get fname and pid , but instead i want to use pid to get pname and display fname and pname

You can do one thing if you want output like this.
Instead of using left join you can use simply join.
I have checked it out so try this code.
select s.fname, p.p_name
from students s
join assignment a on s.s_id = a.s_id
join professor p on a.p_id = p.p_id
Hope you will get right result.

I was hoping to create a view of assignment and professor tables with columns aid, sid, pname and combining them as assignment.pid=professor.pid .
Later I would use left join of student table on the view ....
What do you guys think ? Please chime in your thoughts ....
Thanks

Related

Write a query to find the students with the same first name AND last name?

So here is what I have tried:
SELECT A.STUDENT_ID
FROM STUDENT A, STUDENT B
WHERE A.FNAME=B.FNAME
AND A.LNAME=B.LNAME
AND A.STUDENT_ID!=B.STUDENT_ID;
Here is the STUDENT table columns:
STUDENT_ID primary key
FNAME
LNAME
It seems to work. But according to the practice exam, it is worth 15 points so I am not sure if it is totally right. What if there more that two students have the same fname and lname? How would I write it or would this work for that too?
To avoid the Cartesian product (which is what you get with those multiple FROM clauses) and the duplicate rows that would create, I'd use an EXISTS clause
SELECT a.STUDENT_ID, a.FNAME, a.LNAME
FROM STUDENT a WHERE EXISTS (
SELECT 1 FROM STUDENT b
WHERE a.FNAME = b.FNAME
AND a.LNAME = b.LNAME
AND a.STUDENT_ID <> b.STUDENT_ID
);
http://sqlfiddle.com/#!9/75fd6/3
You would also benefit greatly by having an index on both FNAME and LNAME but I doubt that's going to be relevant to your practice exam.
What you have will technically work. However, the number of rows returned per matched student will equal n - 1, where n is the number of students who have the same first and last name as the matched student.
To address this, use a DISTINCT clause in your query.
SELECT DISTINCT A.STUDENT_ID
FROM STUDENT A, STUDENT B
WHERE A.FNAME=B.FNAME
AND A.LNAME=B.LNAME
AND A.STUDENT_ID!=B.STUDENT_ID;

I want to view many fields from 3 table to show them in same page and same query

Hi
I have this three table . and i have form to put the id_student then the view must be :
the name + surname +father then
the subjects and the mark of each subjects
how i can do that ?
i try this :
select students.name, grade.marks
from students
INNER JOIN grade ON students.id_students = grade.id_students
You are almost there, you need to join with the third table as well. Considering you want all the column from 3 table do something like
select students.*,
grade.*,
Courses.*
from grade
INNER JOIN courses on grade.id_subject = courses.id_subject
INNER JOIN students ON students.id_students = grade.id_students
WHERE students.id_students=690

MySQL get count from two related tables

I am using MySQL and database server and I have two tables one is for customer and another for customer_contacts.
Here are the table structures:
customer(
id(pk, ai)
name
email
)
and
customer_contacts(
id(ai)
customer_id
first_name
last_name
)
Now my question is:
lets say I have one customer has many customer_contacts like this
customer
id name email
1 john john#example.com
and customer_contacts is like this (first row)
id customer_id first_name last_name
1 1 john doe
2 1 johnp pual
like this
So here I want to get all the contact details count for the id john. So can some one tell me how to get that?
Any help and suggestions will be really appreciable. Thanks
If the id is already known (as suggested by the question, it would be a plain
SELECT COUNT(*) from customer_contacts
WHERE customer_contacts.id = 1;
You just need to know the number of contactacs for the customer John, right?
SELECT COUNT(*)
FROM customer cus INNER JOIN customer_contacts con ON cus.id = con.customer_id
WHERE cus.name = 'john'
Nevertheless, It would be better if you know the id of John. Your query would be this:
SELECT COUNT(*)
FROM customer_contacts
WHERE customer_id = 1
Simply join both tables and use count() with group by c.id use where to filter records
select c.*,
count(*) total_contacts
from customer c
left join customer_contacts cc on(c.id = cc.customer_id)
group by c.id
Fiddle Demo

Mysql join query on multiple tables

I have three tables and in those tables these basic fields
contacts
con_id (primary key)
con_name
con_work_id
con_country_id
work
work_id (primary key)
work_company_name
work_country_id
country
country_id (primary key)
country_name
I'm trying to run a query which displays the con_name, work_company_name and then the country name for BOTH the contact and the work company.
I've tried this;
SELECT *
FROM contacts
LEFT JOIN work ON contacts.con_work_id = work.work_id
LEFT JOIN country ON contacts.con_country_id = country.country_id
LEFT JOIN country ON work.work_country_id = country.country_id
But of course this doesn't work because the last join causes a clash with the second one.
I'm almost there, but can't get the query to display the country_name associated with both the contact AND the work company.
I'd appreciate a way forward.
Many thanks,
Wonder
The following should work:
SELECT *
FROM contacts
LEFT JOIN work ON contacts.con_work_id = work.work_id
LEFT JOIN country c1 ON contacts.con_country_id = c1.country_id
LEFT JOIN country c2 ON work.work_country_id = c2.country_id
The trick is to add an alias to the table, so that it is possible to distinguish the two.

Searching on one table with multiple parameters query

I have 2 tables such as below:
Table 1 contain UserID and Name while table 2 contain UserID, Skill_ID, and SkillName
What I want to do is, I want to search someone who has 2 skill, searched via SkillName.
example: I have Andy as Name where he have 2 skill PHP and C# so when i Search PHP & C#, Andy will be shown as result.
Can anybody help me? Thank you.
SELECT a.Name
FROM table1 a
INNER JOIN table2 b
ON a.userID = b.user_ID
WHERE b.skill_name IN ('PHP','C#')
GROUP BY a.Name
HAVING COUNT(*) = 2
the design of your table is not well normalized. The type of relationship is Many-to-Many and in this case, this should be three tables, Users, Skills and the linking table User_Skills.
A suggested design would be:
Table User
UserID (PK)
Name
Table Skills
SkillID (PK)
SkillName
Table User_Skills
UserID (FK)
SkillID (FK)