dpylt7626401 2011-08-14 22:18
浏览 152

MySQL查询:生成与当前用户在同一所在学校的同一班级中的所有人员

I'm attempting to grab results from 2 tables, one that has the following structure:

CREATE TABLE `users` 
(
    `id` bigint (20) NOT NULL AUTO_INCREMENT , 
    `firstname` varchar (255) NOT NULL, 
    `lastname` varchar (255) NOT NULL, 
    `sex` varchar (255) NOT NULL, 
    `orientation` varchar (50), 
    `email` varchar (255) NOT NULL, 
    `altemail` varchar (255), 
    `username` varchar (255) NOT NULL, 
    `password` varchar (255) NOT NULL, 
    `school` varchar (255) NOT NULL, 
    `age` date NOT NULL, 
    `image1` varchar (255), 
    `image2` varchar (255), 
    `image3` varchar (255), 
    `image4` varchar (255), 
    `image5` varchar (255), 
    `music` longtext, 
    `movies` longtext, 
    `tv` longtext, 
    `books` longtext, 
    `saysomething` longtext, 
    `course` varchar (255), 
    `YEAR` integer (11), 
    `verified` varchar (255), 
    `verifieddatetime` datetime, 
    `string` varchar (255), 
    `alerts` varchar (50),
    PRIMARY KEY (`id`)
) 

And one that has the following structure:

CREATE TABLE `timetable` 
(
    `id` bigint (20) NOT NULL AUTO_INCREMENT , 
    `studentid` varchar (255), 
    `classno` varchar (50), 
    `classname` varchar (255), 
    `classnumber` varchar (255), 
    `prof` varchar (255),
    PRIMARY KEY (`id`)
)

The username is stored as a foreign key in the timetable table. Each student enters in their courses in the timetable table. Attempting to do a search results that would generate all the people that are in the same classes at the same school as the current user. Need to return the id, firstname, lastname,image1 fields from the users table and the classname, classnumber of the timetable table. I have the following query to grab results from the users at the same school:

$query = "select users.id, users.firstname, users.lastname, users.age, users.image1, users.school, category.category from users, category where users.image1 !='male.gif' and users.image1 !='female.gif' and users.verified ='yes' and users.username !='$_SESSION[myusername]' and users.school = '$school' and users.school = category.cat_id"; 

But I cannot figure out how to grab the classes as well.

  • 写回答

2条回答 默认 最新

  • douwei1174 2011-08-14 22:26
    关注
    SELECT
      users.id,
      users.firstname,
      users.lastname,
      users.age, 
      users.image1,
      users.school,
      category.category,
      timetable.classname,
      timetable.classnumber
    FROM
      users
    INNER JOIN
      category
    ON
      users.school = category.cat_id
    INNER JOIN
      timetable
    ON
      timetable.studentid = users.id
    INNER JOIN
      (
      SELECT
        classno
      FROM
        timetable
      WHERE
        studentid = $id_of_the_user
      ) classes_of_the_one_user
    ON
      classes_of_the_one_user.classno = timetable.classno
    WHERE
      users.image1 != 'male.gif'
    AND
      users.image1 != 'female.gif'
    AND
      users.verified = 'yes'
    AND
      users.school = '$school'
    
    评论

报告相同问题?

悬赏问题

  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?