如何使用增量自定义字段将行从一个表复制到另一个表

时间:2014-06-11 13:54:40

标签: php mysql

我正在尝试将行从一个表复制到另一个表 表格:首先是bollywood_videos_temp,其他是bollywood_videos
下面是复制行的查询(Works Fine):

   INSERT INTO `bollywood_videos`
    (`category`,
    `category_name`,
    `youtube_link`,
    `title`,
    `alias`,
    `short_description`,
    `long_description`,
    `youtube_video_id`,
    `original_poster`,
    `web_large_poster`,
    `web_small_poster`,
    `app_big_poster`,
    `app_small_poster`,
    `related_videos`,
    `sequence`,
    `status`,
    `published_date`,
    `created_by`,
    `date`)
    SELECT a.`category`,
    b.`type`,
    a.`youtube_link`,
    a.`title`,
    a.`alias`,
    a.`short_description`,
    a.`long_description`,
    a.`youtube_video_id`,
    a.`original_poster`,
    a.`web_large_poster`,
    a.`web_small_poster`,
    a.`app_big_poster`,
    a.`app_small_poster`,
    a.`related_videos`,
    ((SELECT MAX(sequence) AS sequence FROM bollywood_videos)) AS `sequence`,
    a.`status`,
    a.`published_date`,
    a.`created_by`,
    a.`date`
    FROM `bollywood_videos_temp` a
    JOIN bollywood_videos_categories b
    ON a.category=b.id
    WHERE a.`transaction_id`='2_1402492848'  

现在我的问题是,我从上面的子查询(SELECT MAX(sequence) AS sequence FROM bollywood_videos AS sequence)获得与下面附图中相同的序列整数,其中我试图从表bollywood_videos获取最大序列
有没有这样的查询可以从每一行给出MAX序列,我在过去5个小时内遇到了这个问题..帮助将不胜感激! enter image description here

1 个答案:

答案 0 :(得分:2)

通过添加1

来增加序列
((SELECT MAX(sequence)+1 AS sequence FROM bollywood_videos)) AS `sequence`

INSERT INTO `bollywood_videos`
    (`category`,
    `category_name`,
    `youtube_link`,
    `title`,
    `alias`,
    `short_description`,
    `long_description`,
    `youtube_video_id`,
    `original_poster`,
    `web_large_poster`,
    `web_small_poster`,
    `app_big_poster`,
    `app_small_poster`,
    `related_videos`,
    `sequence`,
    `status`,
    `published_date`,
    `created_by`,
    `date`)
    SELECT a.`category`,
    b.`type`,
    a.`youtube_link`,
    a.`title`,
    a.`alias`,
    a.`short_description`,
    a.`long_description`,
    a.`youtube_video_id`,
    a.`original_poster`,
    a.`web_large_poster`,
    a.`web_small_poster`,
    a.`app_big_poster`,
    a.`app_small_poster`,
    a.`related_videos`,
    ((SELECT MAX(sequence)+1 AS sequence FROM bollywood_videos)) AS `sequence`,
    a.`status`,
    a.`published_date`,
    a.`created_by`,
    a.`date`
    FROM `bollywood_videos_temp` a
    JOIN bollywood_videos_categories b
    ON a.category=b.id
    WHERE a.`transaction_id`='2_1402492848'