当我使用JOIN创建新表cover_art
时,我需要向我引用news
的其中一个表添加一个新列newsdetails
,但我不记得我是怎么做的做到了。
这是包含artistid
表中引用的标识artists
的表:
CREATE TABLE `news` (
`post_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`artistid` smallint(5) unsigned NOT NULL,
`title` varchar(150) NOT NULL,
`cover_art` varchar(150) NOT NULL,
`blog_entry` text NOT NULL,
`updated` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
`rating` varchar(150) NOT NULL,
PRIMARY KEY (`post_id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=76 ;
INSERT INTO `news` (`post_id`, `artistid`, `title`, `cover_art`, `blog_entry`, `updated`, `rating`) VALUES
(1, 1, 'Ultra', 'images/temp_cover.jpg', 'The very first track Barrel Of A Gun is blah blah.', '2012-11-19 16:23:19', '990,568'),
(2, 13, 'Mish Mash', 'images/temp_cover.jpg', 'A train pulls into a station and etc etc ad infinitum.', '2012-11-19 16:23:44', '831,880'),
这是artists
表,其中包含我在news
中包含的ID:
CREATE TABLE `artists` (
`id` smallint(5) unsigned NOT NULL AUTO_INCREMENT,
`artists_name` varchar(150) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=170 ;
INSERT INTO `artists` (`id`, `artists_name`) VALUES
(1, 'Depeche Mode'),
(2, 'Amon Tobin'),
Etc.
我已经考虑过做内部和左边的连接,但不能为我的生活记住我做了什么来创建这个newsdata
表,将artistid
转换为artists_name
:< / p>
1 post_id smallint(5)UNSIGNED No 0
2 title varchar(150)latin1_swedish_ci否无
3 blog_entry text latin1_swedish_ci否无
4更新时间戳无00:00:00
5等级varchar(150)latin1_swedish_ci否无
6 artists_name varchar(150)latin1_swedish_ci否无
感谢eggyal让我知道外国钥匙是什么 - 即不要这样做: - (
答案 0 :(得分:0)
SELECT news.post_id,
news.title,
news.blog_entry,
news.updated,
news.rating,
artists.artists_name
FROM news JOIN artists ON artists.id = news.artistid