Searching on one table with multiple parameters query - mysql

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)

Related

How to find out results for not matching particular condition in SQL from multiple tables?

I have 3 tables :
Person table stores basic person wise details with ID as primary Key
This person can have relationships (father / mother etc), which are saved in Relationship table, however the users for them are created in Person table (e.g. ID = 2,3 in person table), This way we know that 2,3 are related to user 1 (carry).
We also have 3rd table - address, which store user ID wise addresses.(for both a user and his related persons, who are also users)
I want to find out if an address exists for either a user or for his related users in SQL. How to achieve this ?
You can combine two rules and search on the combined table as below
SELECT * FROM
(
SELECT username,id,Address.Address
FROM Person
INNER JOIN Address ON Person.id = Address.Userid
UNION ALL
SELECT username,id,Address.Address
FROM Person
INNER JOIN Relationship ON Relationship.Relatedid = Person.id
INNER JOIN Address ON Relationship.Userid = Address.Userid
) as RES
WHERE Address = 'xyz road'
Also you can find DBFiddle link to workout
Query:
select p.id,p.username,(case when a.userid is null then 'No' else 'Yes'end) IsAddressAvailable
from Person p
left join Address a on p.id=a.Userid
Output:
id
username
IsAddressAvailable
1
Carry
Yes
2
Carry-Father
No
3
Carry-Mother
Yes
db<fiddle here

How to select from 2 different tables and insert the values into a single table

Good day people. I would be very glad if someone puts me through this hassle.
I have three tables
employee:
id firstname lastname birthdate
1 John Smith 1976-01-02
2 Laura Jones 1969-09-05
3 Jane Green 1967-07-15
borrowed:
ref book
1 Simply SQL
2 Ultimate HTML Reference
3 Ultimate CSS Reference
4 Art and Science of JavaScript
history:
Firstname Book
My question is this: How can i select the first name from table 1 and the book from table 2 and then insert the result into table 3.
I would be glad if this problem can be resolved for me cause it has really given me stress. Am new to mysql though. Thanks alot.
Assuming that the ref column in the borrowed table relates to the employee ID in the first table, then you can join these tables and do an insert:
INSERT INTO history (Firstname, Book)
SELECT t1.firstname, t2.book
FROM employee t1
INNER JOIN borrowed t2
ON t1.id = t2.ref
Assuming if there are no columns to join
insert into history (Firstname, Book)
SELECT employee.firstname, borrowed.book
FROM employee CROSS JOIN borrowed

Joining two tables and linking the third table

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

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

Joining Tables w/out foreign key

Is it possible to connect tables without foreign key?
For example
tblstudent
Columns:
id
firstname
lastname
middlename
tblgrade
Comluns:
id
quiz
project
exam
tblfinalgrade
Columns:
firstname
lastname
finalgrade
Is it possible to view final grade when searching for and id?
The id in tblStudent is meaningless because you are not referencing it in your other tables. Change your table structure to include this StudentId rather than First Name and Last Name.
For example:
tblGrade
columns:
GradeId
StudentId
Quiz
Project
Exam
tblFinalGrade
FinalGradeId
StudentId
FinalGrade
You can then do:
SELECT ID, FirstName, LAstName, Quiz, Project, Exam, FinalGrade
FROM tblStudent
INNER JOIN tblGrade ON tblGrade.StudentId = tblStudent.StudentId
INNER JOIN tblFinalGrade ON tblFinalGrade.StudentId = tblStudent.StudentId
This would be a better structure than joining on FirstName and Last Name just incase you ever have 5 John Smith how do you know you are returning the correct grades?
Although I am slightly against your original design, you can perform the same query with your existing structure by running the following query:
SELECT ID, FirstName, LAstName, Quiz, Project, Exam, FinalGrade
FROM tblStudent
INNER JOIN tblFinalGrade ON tblFinalGrade.FirstName = tblStudent.FirstName AND tblFinalGrade.LastName = tblStudent.LastName
WHERE tblStudent.ID = 1