Mysql 从两个现有表创建一个新表
我需要在使用JOIN创建新表Mysql 从两个现有表创建一个新表,mysql,join,left-join,inner-join,create-table,Mysql,Join,Left Join,Inner Join,Create Table,我需要在使用JOIN创建新表newsdestails时引用的其中一个表中添加一个新列cover\u art,但我不记得是怎么做的 这是一个包含idartistid的表格,该id在artists表格中引用: CREATE TABLE `news` ( `post_id` smallint(5) unsigned NOT NULL AUTO_INCREMENT, `artistid` smallint(5) unsigned NOT NULL, `title` varchar(150) NOT NU
newsdestails
时引用的其中一个表中添加一个新列cover\u art
,但我不记得是怎么做的
这是一个包含idartistid
的表格,该id在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
表,其中包含我在新闻中包含的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\u name
:
1个post_id smallint(5)未签名的编号02 title varchar(150)拉丁语1_瑞典语1_ci无
3博客条目文本拉丁语1瑞典语无
4更新的时间戳编号0000-00-00:00:00
5评级varchar(150)拉丁语1_瑞典语1_ci无
6名艺术家姓名varchar(150)拉丁1瑞典1无
感谢eggyal让我知道外键是什么-也就是说,不要这样做:-(太棒了,谢谢,这比我想的要干净得多,虽然我主要是停留在代码上,将它转换成一个表,我可以从我的网站的php访问。接下来,我会在页面头部添加查询等,以从不同的表中获取数据,但现在我"我正在考虑创建一个主表,我可以通过这种方式提取数据。我目前正在访问一个名为newsdata的表。多亏了你的回答,我现在有了这个功能,但可以将它转换为一个表?选择news.post\u id、news.title、news.cover\u art、news.blog\entry,news.updated,news.rating,artists.artists\u name FROM news加入artists.id=news.artistidBam上的艺术家,您的回答给我留下了一个简单的插件-感谢eggyal创建表bingo SELECT news.post\u id,news.title,news.cover\u art,news.blog\u entry,news.updated,news.rating,artists.artists\u name FROM news加入artists.id=news上的艺术家。artistid@PaulSeattle:根据这样的查询结果创建另一个表是不常见的(只有在您想要创建数据的缓存快照时才真正这样做)。相反,您通常会在PHP中运行这种类型的查询,以根据需要获取结果集。
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