I would like to know what would be the best way of storing multiple check-boxes values for easy search.
Is there any advantage if for a skills table (football, swimming, dancing ....) I assign unique id or should I use a unique string - the exact word to be stored in the database?
If the answer is "numbers" and the total available options are more than "10" should I start increment the id from ten so I can avoid finding 12, 2, 22 for %2% in case storing is made like 14,23,34 / football,swimming,dancing in the user table.
Is there a better approach?
Use 3 tables:
Person table. This contains single-valued attributed such as name, birthday, etc.
Skills table. This contains the name of each skill.
PersonSkills table. This is a relation table that contains the many-to-many relationship between the first two tables. It has two columns: Person_id and Skill_id, which are foreign keys into the first two tables.
To get all the skills, you join the tables:
SELECT person_name, GROUP_CONCAT(skill_name) skills
FROM Person p
LEFT JOIN PersonSkills ps ON p.person_id = ps.person_id
JOIN Skills s ON s.skill_id = ps.skill_id
To get all the people with a particular skill:
SELECT person_name
FROM Person p
JOIN PersonSkills ps ON p.person_id = ps.person_id
JOIN Skills s ON s.skill_id = ps.skill_id
WHERE skill_name = "swimming"
Related
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
I have two tables. These are not them but it is the same principle:
Table:One (artists)
--------------
id (Primary Key)
name
best genre
Table:Two (artist teams)
-------------
id1 (Foreign Key)
id2 (Foreign Key)
I want to select the artist teams where their favorite genres are the same.
My work so far is
SELECT *
FROM Two INNER JOIN One
WHERE ( ).
Im confused as to what to put in the WHERE statement.
I have no idea how to compare the values of the artist's genres to each other!
pseudo code for WHERE:
retrieve id#1's favourite genre
retrieve id#2's favourite genre
compare them
if equal display the related entity from table Two
I've searched for a while looking for a solution and I can't find anything
just like this, I believe it could be a bit a syntax that im missing.
Thanks for any help!
You need multiple joins to the "artists" table:
select t.*, a1.genre
from teams t join
artists a1
on t.id1 = a1.id join
artists a2
on t.id2 = a2.id and a2.genre = a1.genre;
I am having a major problem joining 5 tables because each table only has 1 column in common with only 1 other table.
Here are my tables and columns in each table:
TABLE (COLUMNS)
person (person_id, first_name, last name)
building (building_id, building_name)
room (room_id, room_number, building_id, capacity)
meeting (meeting_id, room_id, meeting_start, meeting_end)
person_meeting (person_id, meeting_id)
OK, now here is what I am trying to do (pasted from a homework assignment):
Construct the SQL statement to find all the meetings that person_id #1 has to attend. Display the following columns:
Person’s first name
Person’s last name
Building name
Room number
Meeting start date and time
Meeting end date and time
Now I know how to join 2 tables but I have no idea how to pull info from 5 different tables like this.
I tried looking up how to do this and it just says to do a UNION command, and I am just learning and have yet to cover that.
As UNION is used to combine the result from multiple SELECT statements into a single result set, you don't need it for this scenario. You have to join all the tables one by one based on their Id.
SELECT P.First_Name, P.Last_Name, B.Building_name, R.Room_Number,
M.Meeting_Start, M.Meeting_End FROM Person P
JOIN Person_Meeting PM ON P.Person_Id = PM.Person_Id
JOIN Meeting M ON PM.Meeting_Id = M.Meeting_Id
JOIN Room R ON M.Room_Id = R.Room_Id
JOIN Building B ON R.Building_Id = B.Building_Id
WHERE P.Person_Id = 1
Problem
A student registers for a course.
Add that courses name, and registration date, to the student_courses that can be identified with the student that was registered
Combine the two tables to show the courses the student is registered to.
How should I do this, what is the relationship the Student and student_courses have in order to achieve my goal.
What I would do is add a field to student_courses being the student's personalNumber and also adding the courseId to student_courses. This way it is easy to join all 3 tables
You are on the right track with the student_courses table.
Because the relationship between the student and course can be many-to-many (i.e. multiple students can register for multiple courses) there is a need for a relational table to link a course to the student.
student_courses should store a foreign key reference to the primary key on student and course and include any registration-related fields (like registration date). By doing this you can generate a sql statement that builds a list of all courses the student is related to:
select
sc.registrationdate, c.*
from
student_courses sc
join student s on s.personalNumber = sc.student_id
join courses c on c.courseId = sc.courses_id
where
s.personalNumber = 123;
or if you want to find all the students related to a course:
select
s.personalNumber, s.firstName, s.lastName
from
student_courses sc
join student s on s.personalNumber = sc.student_id
join courses c on c.courseId = sc.courses_id
where
c.courseId = 123;
Hope that helps.
I added a many to many relationship between Courses, and Student, and a new table was generated. In that table then I added a date filed.
I have a table called friends which has id and name and a self join table called friendship which stores the relationship which includes friend_id and friend2_id .
how do i get the names of related friends if a name of a particular frnd is given
example
id name
1 jack
2 kurt
3 jim
and
friendship
f_id f1_id
1 3
So if i give 'jack' i should get jim back
You could do this in one query or two queries, depending on what you want to accomplish.
A simple one could be:
SELECT
f_id,
f1_id
FROM
friendship
WHERE
f_id=1
OR
f1_id=1
And then you can get the specific friends with a statement like:
SELECT name FROM people WHERE id IN(2,3)
Alternative is a self join but the hard part here is that your id might be in both f_id and f1_id so that would need some UNION command or something like (untested):
SELECT
p1.name,
p2.name,
FROM
friendship
INNER JOIN
people AS p1
ON friendship.f_id = people.id
INNER JOIN
people AS p2
ON friendship.f1_id = people.id
WHERE
p1.id=1 OR p2.id=1
I would thoroughly check the speed of these options since they are quite heavy on huge amounts of records. If you measure you need more performance try some alternative. For example when you always put the smallest people.id in f_id and the bigger one in f1_id you might run 2 queries which you union. Alternative is to denormalize a small bit to cache the results if you need them frequently.
It would save you lots of joings for example if you would add the names into the friendship table:
SELECT
f_id,
f1_id,
f_name,
f1_name
FROM
friendship
WHERE
f_id=1
OR
f1_id=1