我的数据库中有以下表格:
CREATE TABLE users (
id int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
username varchar(100) NOT NULL,
rol varchar(100) DEFAULT NULL,
) ENGINE=InnoDB DEFAULT CHARSET=latin1;
CREATE TABLE subjects (
subject_id int(11) NOT NULL AUTO_INCREMENT,
subject text,
PRIMARY KEY (subject_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;
CREATE TABLE users_subjects (
users_subjects_id int(11) NOT NULL AUTO_INCREMENT,
user_id_fk int(11),
subject_id_fk int(11),
FOREIGN KEY(user_id_fk) REFERENCES users(id),
FOREIGN KEY(subject_id_fk) REFERENCES subjects(subject_id),
PRIMARY KEY (users_subjects_id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 AUTO_INCREMENT=1;
在我的代码中,一个注册用户的角色为“老师”,并已将主题分配为“ X”, 可以在表格中看到所有注册学生及其相应学科。
但是,我正在尝试执行SELECT,其中显示了角色为“学生”的用户,并且为他们分配了与注册教师相同的“ X”主题。
我正在尝试通过这种方式进行操作,但无法正常工作:
$sql = "SELECT * FROM users where rol ='student' and id in (select distinct u.id from users u,users_subjects us where u.username='".$_SESSION['username']."' and us.user_id_fk=u.id and e.subjects=us.subject_id_fk);";
$result = $conn->query($sql);
有人可以帮我看看SELECT有什么问题吗?谢谢
答案 0 :(得分:1)
您使用别名e
,但未加载。另外,您还要测试subject
和subject_id_fk
SELECT *
FROM users
where rol = 'student'
and id in (
select distinct u.id
from users u, users_subjects us, subjects e
where u.username='".$_SESSION['username']."'
and us.user_id_fk=u.id
and e.subject_id=us.subject_id_fk
);
但是,如果他的职责是学生,则此查询将为您提供与$ _SESSION ['username']相关的用户行。我猜您正在寻找的好查询是以下一个:
SELECT *
FROM users u
JOIN users_subjects us ON (us.user_id_fk = u.id)
JOIN subjects s ON (s.subject_id = us.subject_id_fk)
WHERE u.username = '".$_SESSION['username']."'
AND u.rol = 'student'
答案 1 :(得分:1)
/ 选择一个数据库并运行这些演示数据进行测试 /
如果存在subjects
,则删除表;
如果不存在subjects
,请创建表(
subject_id
int(11)NOT NULL AUTO_INCREMENT,
subject
文字,
主键(subject_id
)
)ENGINE = MyISAM AUTO_INCREMENT = 14 DEFAULT CHARSET = latin1;
插入subjects
(subject_id
,subject
)值
(1,“健康”),
(2,“数学”),
(3,“音乐”),
(4,“地理”),
(5,“科学”),
(6,“社会研究”),
(7,“代数”),
(8,“英语”),
(9,“物理”),
(10,“新闻”),
(11,'几何'),
(12,“舞蹈”),
(13,'Dramatics');
如果存在users
,则删除表;
如果不存在users
,请创建表(
id
int(11)NOT NULL AUTO_INCREMENT,
username
varchar(100)NOT NULL,
rol
varchar(100)默认为空,
主键(id
)
)ENGINE = MyISAM AUTO_INCREMENT = 97 DEFAULT CHARSET = latin1;
插入users
(id
,username
,rol
)值
(1,“ Zacharys”,“ teacher”),
(2,“ Xanders”,“ teacher”),
(3,'Brendans','teacher'),
(4,'Jermaines','teacher'),
(5,“ Danes”,“ teacher”),
(6,'Sonias','teacher'),
(7,'Maliks','teacher'),
(8,'Willas','teacher'),
(9,'Jeromes','teacher'),
(10,“西蒙斯”,“老师”),
(11,'Ferriss','teacher'),
(12,'Hoyts','teacher'),
(13,'Jacobs','teacher'),
(14,'Chaneys','teacher'),
(15,“ Megans”,“ teacher”),
(16,'Jamals','teacher'),
(17,'Basias','student'),
(18,“ Briannas”,“学生”),
(19,'Logans','student'),
(20,'Hardings','学生'),
(21,'哈萨德','学生'),
(22,“ Shelbys”,“ student”),
(23,'Lucians','student'),
(24,'亚历山大','学生'),
(25,'Ayannas','student'),
(26,'Yuris','student'),
(27,'Carissas','student'),
(28,'Tallulahs','学生'),
(29岁,“但丁”,“学生”),
(30,'Alvins','学生'),
(31,'Salvadors','student'),
(32,'Camerons','student'),
(33,'Lances','student'),
(34,'Cleos','student'),
(35,'Abigails','student'),
(36,'科罗拉多','学生'),
(37,'Leilanis','student'),
(38,'Hops','student'),
(39,'Zephrs','student'),
(40,'象牙','学生'),
(41,'泰勒','学生'),
(42,'Jaspers','student'),
(43,'Jelanis','student'),
(44,'克林顿','学生'),
(45,'Marnys','student'),
(46,'Jeromes','学生'),
(47,'Madisons','student'),
(48,'Kaseems','student'),
(49,'Jennifers','student'),
(50,'McKenzies','student'),
(51,'Dantes','student'),
(52,'校长','学生'),
(53,'Adeles','student'),
(54,'Leroys','student'),
(55,“萨尔瓦多人”,“学生”),
(56,'Marvins','student'),
(57,'Lucians','student'),
(58,'Ignatiuss','student'),
(59,'Chantales','student'),
(60岁,“小不列颠人”,“学生”),
(61,'Leandras','student'),
(62,'奥德修斯','学生'),
(63,“镜头”,“学生”),
(64,'象牙','学生'),
(65,'Hollys','student'),
(66,'Drakes','student'),
(67,'Clarks','student'),
(68,'Dieters','student'),
(69,'Kirstens','student'),
(70,'莱斯特','学生'),
(71,'比佛利山庄','学生'),
(72,'Tads','student'),
(73,'命运','学生'),
(74,'Alisas','student'),
(75,“萨尔瓦多人”,“学生”),
(76,'猎人','学生'),
(77,'Julians','student'),
(78,'Lucys','student'),
(79,'Francescas','student'),
(80,'Ayannas','student'),
(81,'梅肯','学生'),
(82,“所罗门人”,“学生”),
(83,'Jeanettes','student'),
(84,'Audras','student'),
(85,'Victors','student'),
(86,'弗农斯','学生'),
(87,'Marvins','student'),
(88,'Zenas','student'),
(89,'Xanders','student'),
(90,'Gareths','student'),
(91,'Henrys','student'),
(92,'Eagans','student'),
(93,'Alexiss','student'),
(94,'Jelanis','student'),
(95,'Brennas','student'),
(96,'Ivans','student');
如果存在users_subjects
,则删除表;
如果不存在users_subjects
,请创建表(
users_subjects_id
int(11)NOT NULL AUTO_INCREMENT,
user_id_fk
int(11)默认为空,
subject_id_fk
int(11)默认为空,
主键(users_subjects_id
),
键user_id_fk
(user_id_fk
),
键subject_id_fk
(subject_id_fk
)
)ENGINE = MyISAM AUTO_INCREMENT = 201 DEFAULT CHARSET = latin1;
插入users_subjects
(users_subjects_id
,user_id_fk
,subject_id_fk
)值
(1、48、12),
(2,19,5),
(3,5,8),
(4,69,4),
(5,73,12),
(6,85,11),
(7,33,8),
(8,20,3),
(9,2,7),
(10、30、6),
(11,21,9),
(12,50,13),
(13、14、12),
(14,79,3),
(15、36、9),
(17,72,10),
(18,89,7),
(19,94,8),
(20,56,6),
(21,16,13),
(22,4,6),
(23,29,2),
(24,20,5),
(25,76,4),
(26、53、8),
(27,48,11),
(28,10,13),
(29,13,13),
(30,84,4),
(31,12,6),
(32,46,4),
(33,20,7),
(34,19,3),
(35,62,11),
(36,84,10),
(37,62,4),
(38,94,7),
(39,29,8),
(40,73,6),
(41,75,12),
(42,50,3),
(43,47,6),
(44,73,2),
(45,52,3),
(46,28,6),
(47,81,2),
(48,96,5),
(49,23,4),
(50,57,10),
(51,2,5),
(52,36,4),
(53,29,13),
(54,88,4),
(55,43,11),
(56,69,1),
(57,16,1),
(58、18、13),
(59,20,2),
(60,25,13),
(61,75,8),
(62,32,3),
(63,34,6),
(64,47,9),
(66,15,1),
(67,53,10),
(68,39,8),
(69,91,5),
(70,82,5),
(71,65,11),
(72,86,2),
(73,61,1),
(74,34,2),
(75,64,8),
(76,65,6),
(77,10,8),
(78,78,6),
(79,33,11),
(80,69,5),
(81,51,1),
(82,56,13),
(83,6,7),
(84,77,12),
(85,23,5),
(86,53,7),
(87,71,8),
(88,3,4),
(90,6,13),
(91,54,10),
(92,1,7),
(93,13,4),
(94,55,9),
(95,88,8),
(96,22,12),
(97,19,1),
(98,93,7),
(100,95,13),
(101,8,4),
(102、13、12),
(103,41,2),
(104,6,6),
(105,54,7),
(106,5,11),
(107,58,11),
(108,1,5),
(109,20,10),
(110,66,3),
(111,28,9),
(112,21,2),
(113,91,2),
(114,74,6),
(115,50,7),
(116,70,4),
(117,47,8),
(118、48、2),
(119,20,4),
(120,32,6),
(122,25,2),
(123,42,7),
(124,89,6),
(125,94,3),
(126,27,2),
(127,90,8),
(128,95,3),
(129,86,9),
(130,65,1),
(131,14,4),
(132,61,6),
(133、33、13),
(134,43,4),
(135,17,7),
(136,69,10),
(137,29,5),
(138,43,12),
(139,90,9),
(141,88,3),
(142,45,8),
(143,52,8),
(144,56,8),
(145,72,2),
(146,15,2),
(147,25,9),
(148,91,4),
(149,16,12),
(151,62,10),
(152,64,10),
(153,17,8),
(154,55,4),
(155,19,4),
(157,14,2),
(158,7,9),
(159,90,1),
(160,45,11),
(161,55,12),
(162,35,2),
(163,1,8),
(164,41,3),
(166,28,5),
(167,43,10),
(168,91,7),
(169,77,11),
(170,38,13),
(171,3,9),
(172,72,5),
(174,80,5),
(175,46,7),
(176,87,4),
(177,76,11),
(178,1,9),
(179,34,5),
(180,46,10),
(181,86,13),
(182,53,4),
(183,48,8),
(184,5,3),
(185,58,3),
(186,6,2),
(188,81,9),
(189,75,6),
(190,40,6),
(191,52,4),
(192,65,7),
(193,52,11),
(194,59,3),
(195,10,3),
(196,64,6),
(197,33,3),
(198,3,1),
(199,96,3),
(200,66,8);
/ * 这是我的查询,如果教师ID为“ 5” 我们必须找出学生名单。 其中rol具有两个值“学生”或“老师” 由于我们只需要学生,所以添加了!=
希望它会帮助您...
选择U.username,U.id,USM.subject_id_fk,S.subject FROM users_subject USM LEFT JOIN用户U ON USM.user_id_fk = U.id 左联接主题S ON USM.subject_id_fk = S.subject_id WHERE USM.subject_id_fk IN(从users_subjects中选择US.subject_id_fk US WHERE US.user_id_fk ='5') AND U.rol!='老师'
* /