如何从字符串值中的主表中检索子表数据

时间:2010-09-29 19:59:15

标签: mysql

这是主表格结构

CREATE TABLE IF NOT EXISTS `gf_film` (

  `film_id` bigint(20) NOT NULL AUTO_INCREMENT,

  `user_id` int(20) NOT NULL,

  `film_name` varchar(100) DEFAULT NULL,

  `film_cat` varchar(30) CHARACTER SET latin1 DEFAULT NULL,

  `film_plot` longtext,

  `film_release_date` date DEFAULT NULL,

  `film_post_date` date DEFAULT NULL,

  `film_type` enum('Movie','Tv') CHARACTER SET latin1 DEFAULT 'Movie',

  `film_feature` enum('Y','N') CHARACTER SET latin1 NOT NULL DEFAULT 'N',

  `film_status` enum('ACTIVE','INACTIVE') CHARACTER SET latin1 NOT NULL DEFAULT 'ACTIVE',

  `film_modify` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,

  `film_link_value` varchar(200) NOT NULL,

  `film_post_link` varchar(255) NOT NULL,

  PRIMARY KEY (`film_id`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=21435 ;

这是我在上面的gf_film表与最低gf_actor表之间映射的子表

CREATE TABLE IF NOT EXISTS `gf_film_actor` (

  `film_id` int(20) NOT NULL,

  `actor_id` int(20) NOT NULL,

  KEY `film_id` (`film_id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

这是我必须重新审视演员姓名的另一张表

CREATE TABLE IF NOT EXISTS `gf_actor` (

  `actor_id` bigint(20) NOT NULL AUTO_INCREMENT,

  `actor_name` varchar(100) DEFAULT NULL,

  `actor_desc` longtext CHARACTER SET latin1,

  PRIMARY KEY (`actor_id`),

  UNIQUE KEY `actor_name` (`actor_name`)
) ENGINE=MyISAM  DEFAULT CHARSET=utf8 AUTO_INCREMENT=60963 ;

这是gf_film_poster的架构

CREATE TABLE IF NOT EXISTS `gf_film_poster` (

  `film_id` int(20) NOT NULL,

  `website_poster_url` varchar(255) DEFAULT NULL,

  `original_poster_url` varchar(255) DEFAULT NULL,

  `default_poster_url` varchar(255) DEFAULT 'noposter.gif',

  UNIQUE KEY `film_id` (`film_id`)

) ENGINE=MyISAM DEFAULT CHARSET=latin1;

执行此查询后

SELECT gf_film.film_id                                 ,
       film_name                                       ,
       DATE_FORMAT(film_release_date,'%d') AS DATE     ,
       DATE_FORMAT(film_release_date,'%m') AS month_ori,
       DATE_FORMAT(film_release_date,'%M') AS MONTH    ,
       DATE_FORMAT(film_release_date,'%Y') AS YEAR     ,
       film_release_date                               ,
       film_feature                                    ,
       film_modify                                     ,
       film_post_link                                  ,
       website_poster_url
FROM   gf_film
       LEFT JOIN gf_film_poster
       ON     gf_film.film_id=gf_film_poster.film_id

我从数据库

获取这些结果
film_id,film_name,date,month_ori,month,year,film_release_date,
 film_feature,film_modify,film_post_link,website_poster_url

所以我需要通过将上述查询与gf_actor和gf_film_actor

联系起来,在一行中包含与每个电影相关的演员姓名和actor_id的另一列

1 个答案:

答案 0 :(得分:1)

SELECT   gf_film.film_id                                 ,
         film_name                                       ,
         DATE_FORMAT(film_release_date,'%d') AS DATE     ,
         DATE_FORMAT(film_release_date,'%m') AS month_ori,
         DATE_FORMAT(film_release_date,'%M') AS MONTH    ,
         DATE_FORMAT(film_release_date,'%Y') AS YEAR     ,
         film_release_date                               ,
         film_feature                                    ,
         film_modify                                     ,
         film_post_link                                  ,
         website_poster_url                              ,
         group_concat(gf_actor.actor_name) AS actors
FROM     gf_film
         LEFT JOIN gf_film_poster
         ON       gf_film.film_id=gf_film_poster.film_id
         LEFT JOIN gf_film_actor
         ON       gf_film_actor.film_id = gf_film.film_id
         LEFT JOIN gf_actor
         ON       gf_film_actor.actor_id = gf_actor.actor_id
GROUP BY gf_film.film_id