如何简化这个mysql请求?

时间:2019-07-18 23:47:10

标签: mysql sql database

我正在尝试获取文件的数据,发布该文件的用户的数据以及该文件所涉及的用户的数据。

我所做的(它可以工作,但是很难看,并不能真正获得我可以使用的数据):

SELECT 
    * 
FROM 
    files 
INNER JOIN 
    clients c 
    on files.about_client = c.id 
INNER JOIN 
    users u 
    on u.id = c.user_id 
INNER JOIN 
    staff s 
    on files.published_by = s.user_id 
INNER JOIN 
    users u2 
    ON u2.id = s.user_id 
WHERE 
    files.published_by = 2;

这是完整的数据库,因此您可能会更好地了解:

DROP TABLE IF EXISTS `users`;
CREATE TABLE IF NOT EXISTS `users` (
    `id`        INT             NOT NULL AUTO_INCREMENT
                                PRIMARY KEY,
    `name`      VARCHAR(100)    NOT NULL,
    `surname`   VARCHAR(100)    NOT NULL,
    `email`     VARCHAR(50)     NOT NULL UNIQUE
)
ENGINE   =  InnoDB
COLLATE  =  utf8_unicode_ci;

DROP TABLE IF EXISTS `sections`;
CREATE TABLE IF NOT EXISTS `sections` (
    `id`                INT             NOT NULL AUTO_INCREMENT
                                        PRIMARY KEY,
    `section`           VARCHAR(255)    NOT NULL
)
    ENGINE   = InnoDB
    COLLATE  = utf8_unicode_ci;

DROP TABLE IF EXISTS `jobs`;
CREATE TABLE IF NOT EXISTS `jobs` (
    `id`                INT             NOT NULL AUTO_INCREMENT
                                        PRIMARY KEY,
    `job`               VARCHAR(100)    NOT NULL,
    `section_id`        INT             NOT NULL,

    CONSTRAINT `section_id` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
    ENGINE   = InnoDB
    COLLATE  = utf8_unicode_ci;

DROP TABLE IF EXISTS `clients`;
CREATE TABLE IF NOT EXISTS `clients` (
    `id`                INT         NOT NULL AUTO_INCREMENT
                        PRIMARY KEY,
    `phone`             VARCHAR(30) NOT NULL,
    `age`               INT         NOT NULL,
    `date_of_birth`     DATE        NOT NULL,
    `security_number`   VARCHAR(99) NOT NULL UNIQUE,
    `user_id`           INT         NOT NULL UNIQUE,

    CONSTRAINT `user_id` FOREIGN KEY (`user_id`) REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
    ENGINE   =  InnoDB
    COLLATE  =  utf8_unicode_ci;

DROP TABLE IF EXISTS `staff`;
CREATE TABLE IF NOT EXISTS `staff` (
   `id`                INT             NOT NULL AUTO_INCREMENT
                                       PRIMARY KEY,
   `job_id`            INT             NOT NULL,
   `password`          VARCHAR(255)    NOT NULL,
   `user_id`           INT             NOT NULL UNIQUE,

    CONSTRAINT `staff_user_id`      FOREIGN KEY (`user_id`)     REFERENCES `users` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT `staff_job_id`       FOREIGN KEY (`job_id`)      REFERENCES `jobs` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
    ENGINE   = InnoDB
    COLLATE  = utf8_unicode_ci;

DROP TABLE IF EXISTS `tags`;
CREATE TABLE IF NOT EXISTS `tags` (
      `id`    INT                 NOT NULL AUTO_INCREMENT
                                  PRIMARY KEY,
      `name`  VARCHAR(255)        NOT NULL UNIQUE
)
    ENGINE   = InnoDB
    COLLATE  = utf8_unicode_ci;





DROP TABLE IF EXISTS `categories`;
CREATE TABLE IF NOT EXISTS `categories` (
    `id`            INT             NOT NULL AUTO_INCREMENT
                                    PRIMARY KEY,
    `name`          VARCHAR(255)    NOT NULL UNIQUE,
    `section_id`    INT             NOT NULL,

    CONSTRAINT `categories_section_id` FOREIGN KEY (`section_id`) REFERENCES `sections` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
    ENGINE   = InnoDB
    COLLATE  = utf8_unicode_ci;

DROP TABLE IF EXISTS `files`;
CREATE TABLE IF NOT EXISTS `files` (
    `id`                INT             NOT NULL AUTO_INCREMENT
                                        PRIMARY KEY,
    `published_by`      INT             NOT NULL,
    `about_client`      INT             NOT NULL,
    `creation_date`     DATETIME        NOT NULL DEFAULT CURRENT_TIMESTAMP,
    `modification_date` DATETIME        NULL
                                        ON UPDATE       CURRENT_TIMESTAMP,
    `path`              VARCHAR(255)    NOT NULL,
    `title`             VARCHAR(100)    NOT NULL UNIQUE,
    `category`          INT             NOT NULL,
    `type`              VARCHAR(30)     NOT NULL,
    `size`              INT             NOT NULL,

    CONSTRAINT `published_by_fk`    FOREIGN KEY (`published_by`)    REFERENCES `users` (`id`)       ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `about_client_fk`    FOREIGN KEY (`about_client`)    REFERENCES `clients` (`id`)     ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `category_fk`        FOREIGN KEY (`category`)        REFERENCES `categories` (`id`)  ON DELETE CASCADE ON UPDATE CASCADE
)
    ENGINE   = InnoDB
    COLLATE  = utf8_unicode_ci;


DROP TABLE IF EXISTS `map_file_staff`;
CREATE TABLE IF NOT EXISTS `map_file_staff` (
    `id`                INT             NOT NULL AUTO_INCREMENT
                                        PRIMARY KEY,
    `file_id`           INT             NOT NULL,
    `staff_id`          INT             NOT NULL,

    CONSTRAINT `map_file_staff_file_fk`     FOREIGN KEY (`file_id`)     REFERENCES `files` (`id`) ON DELETE CASCADE ON UPDATE CASCADE,
    CONSTRAINT `map_file_staff_staff_fk`    FOREIGN KEY (`staff_id`)    REFERENCES `users` (`id`) ON DELETE CASCADE ON UPDATE CASCADE
)
    ENGINE   = InnoDB
    COLLATE  = utf8_unicode_ci;

DROP TABLE IF EXISTS `map_file_tag`;
CREATE TABLE IF NOT EXISTS `map_file_tag` (
    `id`                    INT             NOT NULL AUTO_INCREMENT
                                            PRIMARY KEY,
    `file_id`               INT             NOT NULL,
    `tag_id`                INT             NOT NULL,

    CONSTRAINT `map_file_tag_file_id`    FOREIGN KEY (`file_id`)    REFERENCES `files` (`id`) ON UPDATE CASCADE ON DELETE CASCADE,
    CONSTRAINT `map_file_tag_tag_id`     FOREIGN KEY (`tag_id`)     REFERENCES `tags` (`id`) ON UPDATE CASCADE ON DELETE CASCADE
)
    ENGINE   =  InnoDB
    COLLATE  =  utf8_unicode_ci;

我还不能发布图片(需要10个信誉),所以这里是链接: https://i.imgur.com/vlGVnaV.png

最终目标是在有关该文件的引导卡中显示数据,例如包含文件而不是post的博客索引页面:)

0 个答案:

没有答案