0

In mysql database I have 5 tables stored. I am using php for storing and retrieving data. I would like to retrieve/echo/display information of those 5 tables but in a linked manner. I have academy_id as the foreign key on each table. Each academy has a contact person. Some academies may share the same contact person. But not sure how I can display each academy with its unique information. How can I can display these values through php/mysql select query?

Academy Name | MOU_ID | Academy_ID | STATUS | Academy Created | Course Name | Course Start Date | Instructor First Name | Contact First Name

Tables

CREATE TABLE IF NOT EXISTS `academies` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(25) NOT NULL,
  `mou_id` int(11) default NULL,
  `academy_id` int(11) NOT NULL,
  `status` enum('INACTIVE','ACTIVE') NOT NULL default 'ACTIVE',
  `created_date` date NOT NULL
  PRIMARY KEY  (`id`)
);

INSERT INTO `courses` (`course_id`, `course_name`) VALUES
(1, 'MATH'),
(2, 'ENGLISH'),
(3, 'BIOLOGY'),

CREATE TABLE IF NOT EXISTS `academy_courses` (
  `unique_id` int(11) NOT NULL auto_increment,
  `academy_id` int(11) NOT NULL,
  `course_id` int(11) NOT NULL,
  `start_date` date default NULL,
  PRIMARY KEY  (`unique_id`),
  KEY `course_id` (`academy_id`,`course_id`)
);


CREATE TABLE IF NOT EXISTS `instructors` (
  `instructor_id` int(11) NOT NULL auto_increment,
  `academy_id` int(11) NOT NULL,
  `instructor_fname` varchar(50) NOT NULL
  PRIMARY KEY  (`instructor_id`),
  KEY `academy_id` (`academy_id`)
);


CREATE TABLE IF NOT EXISTS `main_contact` (
  `contact_id` int(11) NOT NULL auto_increment,
  `academy_id` int(11) NOT NULL,
  `contact_fname` varchar(50) NOT NULL,
  PRIMARY KEY  (`contact_id`),
  KEY `academy_id` (`academy_id`)
);

CREATE TABLE IF NOT EXISTS `main_contact_bridge` (
  `academy_id` int(11) NOT NULL,
  `contact_id` int(11) NOT NULL,
  PRIMARY KEY  (`contact_id`,`academy_id`),
  KEY `academy_id` (`academy_id`)
);
4

3 回答 3

1

我认为这就是你想要得到的:

SELECT a.name, a.mou_id, a.academy_id, a.status, a.created_date, ac.course_id, ac.start_date, i.instructor_fname, mc.contact_fname
FROM academy_courses ac 
LEFT JOIN academies a USING (academy_id) 
LEFT JOIN instructors i USING (academy_id) 
LEFT JOIN main_contact mc USING (academy_id);
于 2013-11-13T16:41:29.797 回答
1

我会将instructor_id 添加到academy_courses,否则如果您有多个讲师,您将获得每个讲师的所有重复数据。然后你会这样做:

SELECT a.name AS 'Academy Name', 
       a.mou_id AS 
       a.academy_id
       a.status
       a.created_date AS 'Academy Created',
       c.course_name AS 'Course Name',
       ac.start_date AS 'Course Start Date',
       i.instructor_fname AS 'Instructor First Name',
       co.contact_fname AS 'Contact First Name'
FROM academies a
INNER JOIN academy_courses ac ON a.id = ac.academy_id
INNER JOIN courses c ON c.id = ac.course_id
INNER JOIN instructors i ON i.instructor_id = ac.instructor_id
INNER JOIN main_contact co ON co.academy_id = a.id

如果您想从 Academy 获取信息,即使课程/academy_courses/etc 不存在,您也可以编写一个 LEFT JOIN。

于 2013-11-13T17:12:02.560 回答
0

您正在寻找的是加入表格。有不同类型的连接。请看一下这个解释http://www.codinghorror.com/blog/2007/10/a-visual-explanation-of-sql-joins.html

于 2013-11-13T16:38:24.420 回答