如何获取唯一列的id

时间:2015-05-29 14:54:57

标签: php mysql

  1. 插入文章并在其中找到标签
  2. 逐个插入一个标签和一个参考,以了解标签
  3. 的last_insert_id()
    $tags = array('strawberries','blueberries','food','strawberries')
    
    function hashTagInsert($tags) 
        {
            /// commit this as a transaction
    
            $lastInsertId = $this->db->lastId(); // last insert id of article
    
            foreach ($tags as $val)
            {
                $sql = 'INSERT INTO tags (tag, added) VALUES (:tag, NOW()) ON DUPLICATE KEY UPDATE occurrence = occurrence + 1';
                $this->db->boolQuery($sql, array(':tag' => $val));
    
                $sql2 = 'INSERT INTO tags_refs (article_id, tag_id) VALUES (:lastInsertId, LAST_INSERT_ID())';
                $this->db->boolQuery($sql2, array(':lastInsertId' => $lastInsertId));
            }
        }
    

    我写了这样的脚本来照顾它。它的工作原理。遗憾的是,由于存在唯一的列,所以重复性存在一些问题。

    如果存在双重性,那么原始标签就是原样,我不知道它具有的ID。在之后的另一个表中,我得到了最后一个不存在的插入标记的新引用。

    标签存在,但引用错误。

    在第一个处理脚本之后,DB看起来像这样:

    CREATE TABLE `tags` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `tag` varchar(100) NOT NULL,
      `occurrence` int(11) NOT NULL,
      `added` datetime NOT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `tag` (`tag`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    
    INSERT INTO `tags` (`id`, `tag`, `occurrence`, `added`) VALUES
    (18,    'boobs',    1,  '2015-05-29 16:46:44'),
    (20,    'food', 0,  '2015-05-29 16:46:44'),
    (19,    'blueberries',  0,  '2015-05-29 16:46:44');
    
    and the reference one:
    
        DROP TABLE IF EXISTS `tags_refs`;
    CREATE TABLE `tags_refs` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `article_id` int(11) NOT NULL,
      `tag_id` int(11) NOT NULL,
      PRIMARY KEY (`id`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8;
    
    INSERT INTO `tags_refs` (`id`, `article_id`, `tag_id`) VALUES
    (24,    31, 18),
    (25,    31, 19),
    (26,    31, 20),
    (27,    31, 26);
    
    -- 2015-05-29 14:49:55
    

1 个答案:

答案 0 :(得分:0)

function hashTagInsert($tags) 
{
    /// commit this as a transaction

    $lastInsertId = $this->db->lastId();

    foreach ($tags as $val)
    {
        $sql = 'INSERT INTO tags (tag, added) VALUES (:tag, NOW()) ON DUPLICATE KEY UPDATE occurrence = occurrence + 1, last_article_id = :lastInsertId';
        $stmt = $this->db->boolQuery($sql, array(':tag' => $val,
                                                 ':lastInsertId' => $lastInsertId));

        if(!$stmt)
        {
           $sql3 = 'SELECT id FROM tags WHERE tag = :tag';

           $existingTagId = $this->db->queryFetch($sql3, array(':tag' => $val));
        }

        $tagId = (isset($existingTagId)? $existingTagId : $this->db->lastId());

        $sql2 = 'INSERT INTO tags_refs (article_id, tag_id) VALUES (:lastInsertId, :tagId)';
        $this->db->boolQuery($sql2, array(':lastInsertId' => $lastInsertId,
                                          ':tagId' => $tagId));

        unset($existingTagId);
    }
}