3
SELECT u.users_username, u.givenname, u.familyname, u.studentassent, u.parentconsent, u.birthdate, u.gender
FROM users AS u
JOIN classes_users as c
ON c.users_username = u.users_username
JOIN classes_users as x
ON c.classes_id = x.classes_id
WHERE x.users_username = "johnny" AND x.role = "teacher"

或者

SELECT u.users_username, u.givenname, u.familyname, u.studentassent, u.parentconsent, u.birthdate, u.gender
FROM users AS u
WHERE u.users_username
IN (
    SELECT c.users_username
    FROM classes_users as c
    JOIN classes_users as x
    ON c.classes_id = x.classes_id
    WHERE x.users_username = "johnny" AND x.role = "teacher"
)

我认为第一个更好,但我仍在学习如何编写更好的 SQL 语句,我不清楚在这种情况下会发生什么使一个语句比另一个语句更好的所有内部结构。

如果有比我写的两种方式更好的写法,请告诉我。谢谢。

编辑:有老师和学生。通过查看 classes_users 表可以找到他们在任何给定班级中作为学生或教师的位置。我想要做的是当给一个用户时,找到他是老师的班级,然后返回这些班级的所有学生。

这是我的数据库架构:

-- -----------------------------------------------------
-- Table `kcptech`.`users`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `kcptech`.`users` (
`users_username` VARCHAR(63) NOT NULL ,
`password` VARCHAR(255) NULL DEFAULT NULL ,
`salt` VARCHAR(127) NULL DEFAULT NULL ,
`givenname` VARCHAR(96) NULL DEFAULT NULL ,
`familyname` VARCHAR(128) NULL DEFAULT NULL ,
`privileges` TINYINT NULL DEFAULT NULL ,
`studentassent` TINYINT(1) UNSIGNED NULL DEFAULT NULL ,
`parentconsent` TINYINT(1) UNSIGNED NULL DEFAULT NULL ,
`birthdate` DATE NULL DEFAULT NULL ,
`gender` VARCHAR(1) NULL DEFAULT NULL ,
`registration` TIMESTAMP NULL DEFAULT NULL ,
PRIMARY KEY (`users_username`) ,
UNIQUE INDEX `uname_UNIQUE` (`users_username` ASC) )
ENGINE = InnoDB;




-- -----------------------------------------------------
-- Table `kcptech`.`classes`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `kcptech`.`classes` (
`classes_id` INT UNSIGNED NOT NULL AUTO_INCREMENT ,
`course` VARCHAR(127) NULL ,
`period` VARCHAR(31) NULL DEFAULT '' ,
`organization` VARCHAR(127) NULL DEFAULT '' ,
PRIMARY KEY (`classes_id`) ,
UNIQUE INDEX `id_UNIQUE` (`classes_id` ASC) )
ENGINE = InnoDB;


-- -----------------------------------------------------
-- Table `kcptech`.`classes_users`
-- -----------------------------------------------------
CREATE  TABLE IF NOT EXISTS `kcptech`.`classes_users` (
`classes_id` INT UNSIGNED NOT NULL ,
`users_username` VARCHAR(64) NOT NULL ,
`role` VARCHAR(12) NOT NULL ,
PRIMARY KEY (`classes_id`, `users_username`) ,
INDEX `fk_class_users__users_users_username` (`users_username` ASC) ,
INDEX `fk_class_users__class_class_id` (`classes_id` ASC) ,
CONSTRAINT `fk_class_users__users_users_username`
  FOREIGN KEY (`users_username` )
  REFERENCES `kcptech`.`users` (`users_username` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION,
CONSTRAINT `fk_class_users__class_class_id`
  FOREIGN KEY (`classes_id` )
  REFERENCES `kcptech`.`classes` (`classes_id` )
  ON DELETE NO ACTION
  ON UPDATE NO ACTION)
ENGINE = InnoDB;
4

1 回答 1

5

第一个更好,假设它(classes_id, users_username)是独一无二的。

MySQL不能IN在内部查询前导进行半连接(构造)。因此IN查询将始终users用作前导表,而对于JOIN查询,优化器可以选择前导表。

如果(classes_id, users_username)不是唯一的,则查询在语义上是不等价的。您需要添加DISTINCT到联接查询。

于 2012-05-21T23:28:37.620 回答