简体   繁体   中英

MySQL Query Acress Two Tables, Matching on Two Columns

I have two tables - moduletable and studenttable. I will pass in a value to the query and need to retrieve data from both tables where the passed value matches moduletable.moduleNo and studenttable.moduleNo1 OR moduletable.moduleNo and studenttable.moduleNo2.

Could anybody give me a pointer as to the correct MySQL syntax for this? I tried as an example:

function getStudents($id) { $sql = "select s.studentID, s.firstName, s.lastName, s.moduleNo1, s.moduleNo2, m.moduleNo from studenttable s, moduletable m where m.moduleNo=:id AND s.moduleNo1=:id OR s.moduleNo2=:id";

But I am getting incorrect results. Any help appreciated!

SQL DDL for both tables is:

CREATE TABLE `moduleTable` (

  `moduleNo` int(6) NOT NULL,
  `moduleName` varchar(30) NOT NULL,
  `credits` int(2) NOT NULL,
  `website` varchar(30) NOT NULL,
  `dueDate` date NOT NULL,
  `location` varchar(25) NOT NULL,
  `room` varchar(10) NOT NULL,
  `lat` varchar(20) NOT NULL,
  `long` varchar(20) NOT NULL,
  PRIMARY KEY (`moduleNo`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This table contains all module records for the example database.';

--

-- Dumping data for table moduleTable

INSERT INTO `moduleTable` VALUES(999001, 'Dynamic Web Development', 15, 'www.dynWeb.ie', '2013-05-14', 'Aungier Street', '4037', '53.338545', '-6.26607');
INSERT INTO `moduleTable` VALUES(999002, 'Human Computer Interaction', 10, 'www.hci.ie', '2013-04-09', 'Aungier Street', '2005', '53.338545', '-6.26607');
INSERT INTO `moduleTable` VALUES(999003, 'Introduction to Programming', 15, 'www.jscriptIntro.ie', '2013-01-11', 'Kevin Street', '1045', '53.337015', '-6.267933');
INSERT INTO `moduleTable` VALUES(999004, 'Design Principles', 15, 'www.designIntro.ie', '2013-04-25', 'Bolton Street', '0130', '53.351406', '-6.268724');
INSERT INTO `moduleTable` VALUES(999005, 'Design Practice', 10, 'www.designPract.ie', '2013-01-11', 'Cathal Brugha Street', '0123', '53.352044', '-6.259514');
INSERT INTO `moduleTable` VALUES(999006, 'Digital Audio', 10, 'www.dspAudio.com', '2013-05-10', 'Aungier Street', '3025', '53.338545', '-6.26607');
INSERT INTO `moduleTable` VALUES(999007, 'Digital Signal Processing', 10, 'www.dspGeneral.ie', '2013-04-04', 'Kevin Street', '2103', '53.337015', '-6.267933');
INSERT INTO `moduleTable` VALUES(999008, 'History of Digital Media', 5, 'www.itsbeendone.ie', '2013-03-28', 'Aungier Street', '0120', '53.338545', '-6.26607');
INSERT INTO `moduleTable` VALUES(999009, 'Digital Asset Management', 5, 'www.contentStore.ie', '2013-05-30', 'Bolton Street', '1004', '53.351406', '-6.268724');
INSERT INTO `moduleTable` VALUES(999010, 'Production Skills', 10, 'www.webDevPro.ie', '2013-04-02', 'Aungier Street', '1089', '53.338545', '-6.26607');

--

-- Table structure for table studentTable

CREATE TABLE `studentTable` (
  `studentID` int(6) NOT NULL,
  `firstName` varchar(10) NOT NULL,
  `lastName` varchar(15) NOT NULL,
  `moduleNo1` int(6) NOT NULL,
  `moduleNo2` int(6) NOT NULL,
  `courseID` int(6) NOT NULL,
  PRIMARY KEY (`studentID`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='This table contains all student records for the example database.';

--

-- Dumping data for table studentTable

INSERT INTO `studentTable` VALUES(123, 'Kermit', 'Frog', 999003, 999008, 888001);
INSERT INTO `studentTable` VALUES(124, 'Gonzo', 'Great', 999001, 999009, 888001);
INSERT INTO `studentTable` VALUES(125, 'Cookie', 'Monster', 999004, 999005, 888002);
INSERT INTO `studentTable` VALUES(126, 'Fozzie', 'Bear', 999006, 999010, 888001);
INSERT INTO `studentTable` VALUES(127, 'Bunsen', 'Honeydew', 999007, 999009, 888003);
INSERT INTO `studentTable` VALUES(128, 'Miss', 'Piggy', 999002, 999003, 888003);
INSERT INTO `studentTable` VALUES(129, 'Gobo', 'Fraggle', 999008, 999010, 888002);
INSERT INTO `studentTable` VALUES(130, 'Mokey', 'Fraggle', 999002, 999005, 888001);
INSERT INTO `studentTable` VALUES(131, 'Red', 'Fraggle', 999006, 999008, 888003);
INSERT INTO `studentTable` VALUES(132, 'Wembley', 'Fraggle', 999004, 999007, 888003);
INSERT INTO `studentTable` VALUES(133, 'Travelling', 'Matt', 999002, 999003, 888002);
INSERT INTO `studentTable` VALUES(134, 'Convincing', 'John', 999004, 999008, 888001);
INSERT INTO `studentTable` VALUES(135, 'Cotterpin', 'Doozer', 999008, 999009, 888002);
INSERT INTO `studentTable` VALUES(136, 'Judge', 'Dog', 999003, 999007, 888003);
INSERT INTO `studentTable` VALUES(137, 'Doctor', 'Astro', 999005, 999001, 888001);
INSERT INTO `studentTable` VALUES(138, 'Sneaky', 'Snake', 999006, 999008, 888002);
INSERT INTO `studentTable` VALUES(139, 'Sunni', 'Gummi', 999009, 999010, 888002);
INSERT INTO `studentTable` VALUES(140, 'Cubbi', 'Gummi', 999004, 999008, 888001);
INSERT INTO `studentTable` VALUES(141, 'Papa', 'Smurf', 999008, 999009, 888003);
INSERT INTO `studentTable` VALUES(142, 'Lazy', 'Smurf', 999001, 999002, 888001);
INSERT INTO `studentTable` VALUES(143, 'Vanity', 'Smurf', 999008, 999010, 888002);
INSERT INTO `studentTable` VALUES(144, 'Joe', 'Frasier', 999004, 999006, 888003);
INSERT INTO `studentTable` VALUES(145, 'Muhammad', 'Ali', 999003, 999005, 888002);
INSERT INTO `studentTable` VALUES(146, 'George', 'Foreman', 999002, 999003, 888001);
INSERT INTO `studentTable` VALUES(147, 'Larry', 'Holmes', 999001, 999002, 888001);
INSERT INTO `studentTable` VALUES(148, 'Marvin', 'Hagler', 999004, 999005, 888003);
INSERT INTO `studentTable` VALUES(149, 'John', 'Coltrane', 999002, 999006, 888002);
INSERT INTO `studentTable` VALUES(150, 'Sonny', 'Rawlins', 999009, 999010, 888002);
INSERT INTO `studentTable` VALUES(151, 'Coleman', 'Hawkins', 999006, 999007, 888003);
INSERT INTO `studentTable` VALUES(152, 'Wes', 'Montgomery', 999002, 999004, 888001);
INSERT INTO `studentTable` VALUES(153, 'Joe', 'Pass', 999006, 999009, 888001);
INSERT INTO `studentTable` VALUES(154, 'Charlie', 'Christian', 999008, 999010, 888002);
INSERT INTO `studentTable` VALUES(155, 'Stanley', 'Jordan', 999004, 999007, 888003);
INSERT INTO `studentTable` VALUES(156, 'Rory', 'Gallagher', 999006, 999009, 888003);
INSERT INTO `studentTable` VALUES(157, 'Gary', 'Moore', 999001, 999008, 888002);
INSERT INTO `studentTable` VALUES(158, 'Jimi', 'Hendrix', 999004, 999008, 888001);
INSERT INTO `studentTable` VALUES(159, 'Paco', 'Pena', 999005, 999009, 888003);
INSERT INTO `studentTable` VALUES(160, 'Andres', 'Segovia', 999003, 999007, 888003);
INSERT INTO `studentTable` VALUES(161, 'Bootsy', 'Collins', 999004, 999005, 888002);
INSERT INTO `studentTable` VALUES(162, 'George', 'Clinton', 999003, 999010, 888002);

AND has higher precedence than OR in SQL, so you need parentheses:

$sql = "select s.studentID, s.firstName, s.lastName, s.moduleNo1, s.moduleNo2, m.moduleNo
        from studenttable s, moduletable m
        where m.moduleNo=:id AND (s.moduleNo1=:id OR s.moduleNo2=:id)";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM