Question : sql many to many relationship
Answered by : bhagirathsinh-makwana
--example of many to many relationship in sql with junction table.
CREATE TABLE Students ( Student_ID INT PRIMARY KEY, Student_Name VARCHAR(50) -- Other student-related columns
);
CREATE TABLE Courses ( Course_ID INT PRIMARY KEY, Course_Name VARCHAR(50) -- Other course-related columns
);
CREATE TABLE Student_Course ( Student_ID INT, Course_ID INT, PRIMARY KEY (Student_ID, Course_ID), FOREIGN KEY (Student_ID) REFERENCES Students(Student_ID), FOREIGN KEY (Course_ID) REFERENCES Courses(Course_ID)
);
Source : https://chat.openai.com/c/4ff9fc9c-b807-48a2-8b4e-5d73f3eb40fe | Last Update : Wed, 29 Nov 23
Question : Single SQL query on many to many relationship
Answered by : wandering-wasp-huvmto8t11iz
-- Single SQL query on many to many relationship
-- Example:
SELECT posts.id, posts.title, categories.id, categories.title
FROM posts
JOIN posts_categories ON posts.id = posts_categories.post_id
JOIN categories ON posts_categories.category_id = categories.id
Source : | Last Update : Mon, 29 Aug 22