加入五桌

时间:2014-08-12 09:12:05

标签: mysql join

我尝试通过连接五个表来创建图像的过滤器输出。由于我的where语句,我需要连接五个表。我的sql如下:

SELECT `files`.`id` AS `id`,
    `files`.`name` AS `name`,
    `files`.`size` AS `size`, 
    `files`.`type` AS `type`, 
    `files`.`url` AS `url`, 
    `files`.`title` AS `title`, 
    `files`.`description` AS `description`, 
    `files`.`category` AS `category` 
FROM `files` 
INNER JOIN `folders` 
    ON `files`.`category` = `folders`.`id` 
INNER JOIN `access` 
    ON `access`.`folder_id` = `folders`.`id` 
INNER JOIN `tags_files` 
    ON `tags_files`.`file_id` = `file`.`id` 
INNER JOIN `tags` 
    ON `tags`.`id` = `tags_files`.`tag_id` 
WHERE ( `access`.`user_id` = 11 ) 
    AND ( `files`.`type` = 'application/x-photoshop' 
         OR `files`.`type` = 'image/jpeg' 
         OR `files`.`type` = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' OR type = 'application/download' 
         OR `files`.`type` = 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' 
         OR type = 'application/msword' 
         OR `files`.`type` = 'application/postscript' 
         OR `files`.`type` = 'image/png' 
         OR `files`.`type` = 'application/pdf' 
         OR `files`.`type` = 'image/tiff' 
         OR `files`.`type` = 'image/gif' 
         OR `files`.`type` = 'application/vnd.openxmlformats-officedocument.presentationml.presentation' 
         OR `files`.`type` = 'application/octet-stream' 
         OR `files`.`type` = 'application/zip' 
         OR `files`.`type` = 'text/plain' OR `files`.`type` = 'text/rtf' ) 
    AND ( `tags`.`name` = 'apple' )

我开始时只有三张桌子:

  

文件,文件夹,访问

一切都很顺利但是加了最后两个

  

tag,tags_files

我收到以下错误:

  

1054 - 未知栏' file.id'在' on子句'

这很奇怪,因为那是我开始的桌子......感谢您的帮助!

3 个答案:

答案 0 :(得分:3)

您的请求中没有file.id,但files.id存在,我不确定原因是什么?没有表结构。 。

答案 1 :(得分:0)

您的表名不正确。这就是你得到那个错误的原因。我认为它必须是files.id或使用具有列名

的相应表名

答案 2 :(得分:0)

顺便提一下,这个查询可以重写如下 - 没有性能优势,但我发现它更容易阅读......

 SELECT f.id 
      , f.name 
      , f.size 
      , f.type 
      , f.url 
      , f.title 
      , f.description 
      , f.category 
   FROM files f
   JOIN folders d
     ON d.id = f.category
   JOIN access a
     ON a.folder_id = d.id 
   JOIN tags_files tf
     ON tf.file_id = f.id 
   JOIN tags t
     ON t.id = tf.tag_id 
  WHERE a.user_id = 11 
    AND f.type IN( 'application/x-photoshop'
                 , 'image/jpeg' 
                 , 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet' 
                 , 'application/download' 
                 , 'application/vnd.openxmlformats-officedocument.wordprocessingml.document' 
                 , 'application/msword' 
                 , 'application/postscript' 
                 , 'image/png' 
                 , 'application/pdf' 
                 , 'image/tiff'
                 , 'image/gif'
                 , 'application/vnd.openxmlformats-officedocument.presentationml.presentation' 
                 , 'application/octet-stream'
                 , 'application/zip'
                 , 'text/plain' 
                 , 'text/rtf'
                 ) 
    AND t.name = 'apple';