MySQL查询Acress两个表,在两列上匹配
我有两张桌子——moduletable和studenttable。我将向查询传递一个值,并需要从传递的值与moduletable.moduleNo和studenttable.moduleNo1或moduletable.moduleNo和studenttable.moduleNo2匹配的两个表中检索数据 有谁能给我一个正确的MySQL语法的指针吗?我举了一个例子: 函数getStudents($id){ $sql=“从studenttable s、moduletable m中选择s.studentID、s.firstName、s.lastName、s.moduleNo1、s.moduleNo2、m.ModuleNon 其中m.moduleNo=:id 和s.moduleNo1=:id 或s.moduleNo2=:id“; 但是我得到了错误的结果。任何帮助都将被感激 两个表的SQL DDL都是:MySQL查询Acress两个表,在两列上匹配,mysql,Mysql,我有两张桌子——moduletable和studenttable。我将向查询传递一个值,并需要从传递的值与moduletable.moduleNo和studenttable.moduleNo1或moduletable.moduleNo和studenttable.moduleNo2匹配的两个表中检索数据 有谁能给我一个正确的MySQL语法的指针吗?我举了一个例子: 函数getStudents($id){ $sql=“从studenttable s、moduletable m中选择s.student
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.';
--
--正在转储表的数据moduleTable
-- --表
studentTable的表结构
--
--正在转储表的数据studentTable
和
的优先级高于SQL中的或
,因此需要括号:
$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)";
请使用MaskDand并重构你的问题……你是在要求我们做你的家庭作业吗?如果不是,那么你应该建立一个正确的数据结构和数据。谢谢Gordon . Barmar指出了我的正确方向。
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.';
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);
$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)";