How to Join 3 Tables (Or More) in MYSQL phpMyAdmin (Xampp LocalHost)
Query For Getting Data From Multiple Tables
SELECT
students.name,
students_courses.student_id AS StudentID,
students_courses.course_id AS CourseID,
courses.coursename
FROM students
JOIN students_courses
ON students.id = students_courses.student_id
JOIN courses
ON courses.id = students_courses.course_id
WHERE students.name = 'Jane';
phpMyAdmin SQL Dump
-- phpMyAdmin SQL Dump
-- version 5.1.0
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1
-- Generation Time: May 26, 2021 at 11:35 AM
-- Server version: 10.4.19-MariaDB
-- PHP Version: 8.0.6
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @[email protected]@CHARACTER_SET_CLIENT */;
/*!40101 SET @[email protected]@CHARACTER_SET_RESULTS */;
/*!40101 SET @[email protected]@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `studentcourse`
--
CREATE DATABASE IF NOT EXISTS `studentcourse` DEFAULT CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci;
USE `studentcourse`;
-- --------------------------------------------------------
--
-- Table structure for table `courses`
--
CREATE TABLE `courses` (
`id` int(11) NOT NULL,
`coursename` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `courses`
--
INSERT INTO `courses` (`id`, `coursename`) VALUES(1, 'Computer Science');
INSERT INTO `courses` (`id`, `coursename`) VALUES(2, 'Information Technology');
INSERT INTO `courses` (`id`, `coursename`) VALUES(3, 'Computer Science');
INSERT INTO `courses` (`id`, `coursename`) VALUES(4, 'Information Technology');
-- --------------------------------------------------------
--
-- Table structure for table `students`
--
CREATE TABLE `students` (
`id` int(11) NOT NULL,
`name` varchar(30) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `students`
--
INSERT INTO `students` (`id`, `name`) VALUES(1, 'Jane');
INSERT INTO `students` (`id`, `name`) VALUES(2, 'Jimmy');
-- --------------------------------------------------------
--
-- Table structure for table `students_courses`
--
CREATE TABLE `students_courses` (
`id` int(11) NOT NULL,
`student_id` int(11) NOT NULL,
`course_id` int(11) NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
--
-- Dumping data for table `students_courses`
--
INSERT INTO `students_courses` (`id`, `student_id`, `course_id`) VALUES(1, 1, 1);
INSERT INTO `students_courses` (`id`, `student_id`, `course_id`) VALUES(2, 2, 2);
--
-- Indexes for dumped tables
--
--
-- Indexes for table `courses`
--
ALTER TABLE `courses`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `students`
--
ALTER TABLE `students`
ADD PRIMARY KEY (`id`);
--
-- Indexes for table `students_courses`
--
ALTER TABLE `students_courses`
ADD PRIMARY KEY (`id`),
ADD KEY `Constraint_FK_Student_ID` (`student_id`),
ADD KEY `Constraint_FK_Course_ID` (`course_id`);
--
-- AUTO_INCREMENT for dumped tables
--
--
-- AUTO_INCREMENT for table `courses`
--
ALTER TABLE `courses`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;
--
-- AUTO_INCREMENT for table `students`
--
ALTER TABLE `students`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- AUTO_INCREMENT for table `students_courses`
--
ALTER TABLE `students_courses`
MODIFY `id` int(11) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=3;
--
-- Constraints for dumped tables
--
--
-- Constraints for table `students_courses`
--
ALTER TABLE `students_courses`
ADD CONSTRAINT `Constraint_FK_Course_ID` FOREIGN KEY (`course_id`) REFERENCES `courses` (`id`),
ADD CONSTRAINT `Constraint_FK_Student_ID` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`);
COMMIT;
/*!40101 SET [email protected]_CHARACTER_SET_CLIENT */;
/*!40101 SET [email protected]_CHARACTER_SET_RESULTS */;
/*!40101 SET [email protected]_COLLATION_CONNECTION */;