简体   繁体   English

使用临时表在字段mysql中插入新名称

[英]Inserting new name in field mysql with temporary table

I am creating a duplicate functionality in my application, this duplicates an entire vacancy with the following code, in the example i am duplicating from vacancy id 94 (which has banner name "image.jpg": 我正在应用程序中创建重复功能,这将使用以下代码复制整个空缺,在示例中,我从空缺ID 94(具有横幅名称“ image.jpg”)进行复制:

$vacdata = $this->vacancies_model->get($data['old_vacancy_id']);
$newvacbannername = return_unique_filename($vacdata->banner);
copy_file($vacdata->banner, $newvacbannername);
$data['newvacancyid'] = $this->vacancies_model->duplicate($data['old_vacancy_id'], $newvacbannername);

In the $newvacbannername there is the string "image_8.jpg". $ newvacbannername中有字符串“ image_8.jpg”。 Which is the unique name of my new file name which i copy, the copy is working as intended. 这是我复制的新文件名的唯一名称,该副本正在按预期工作。

The function duplicate looks like this: 函数重复看起来像这样:

function duplicate($vacid, $banner)
{
    $this->db->query('CREATE TEMPORARY TABLE tmptable SELECT * FROM vacancies WHERE vacancy_id = ' . $vacid);
    $this->db->query('UPDATE tmptable SET vacancy_id = NULL');
    $this->db->query('UPDATE tmptable SET banner = ' . $banner );
    $this->db->query('UPDATE tmptable SET create_time = now()');
    $this->db->query('UPDATE tmptable SET watch_counter = 0');
    $this->db->query('UPDATE tmptable SET contact_info_counter = 0');
    $this->db->query('UPDATE tmptable SET status = 0');
    $this->db->query('UPDATE tmptable SET reminder_mail_sent = 0');
    $this->db->query('UPDATE tmptable SET extend_code = NULL');
    $this->db->query('INSERT INTO vacancies SELECT * FROM tmptable');
    $newvacancyid = $this->db->insert_id();
    $this->db->query('DROP TEMPORARY TABLE IF EXISTS tmptable');
    return $newvacancyid;
}

I tried doing a var_dump on $banner to make sure that this variable contains the correct string, and it does contain image_8.jpg. 我尝试在$ banner上执行var_dump,以确保此变量包含正确的字符串,并且确实包含image_8.jpg。 So it just copies the banner from the original vacancy but it does not set the new banner name 因此,它只是从原始空缺中复制横幅,但未设置新的横幅名称

If i look at the SQL that was executed i see the following results: 如果我查看执行的SQL,会看到以下结果:

0.0076 CREATE TEMPORARY TABLE tmptable SELECT * FROM vacancies WHERE vacancy_id = 94 0.0076 CREATE TEMPORARY TABLE TMPTABLE SELECT * FROM职位空缺vacancy_id = 94

0.0090 UPDATE tmptable SET vacancy_id = NULL 0.0090更新tmptable SET vacancy_id = NULL

0.0000 UPDATE tmptable SET banner = image_8.jpg 0.0000更新tmptable设置标题= image_8.jpg

0.0005 UPDATE tmptable SET create_time = now() 0.0005 UPDATE tmptable SET create_time = now()

0.0023 UPDATE tmptable SET watch_counter = 0 0.0023 UPDATE tmptable SET watch_counter = 0

0.0001 UPDATE tmptable SET contact_info_counter = 0 0.0001 UPDATE tmptable SET contact_info_counter = 0

0.0002 UPDATE tmptable SET status = 0 0.0002 UPDATE tmptable SET状态= 0

0.0002 UPDATE tmptable SET reminder_mail_sent = 0 0.0002 UPDATE tmptable SET hinter_mail_sent = 0

0.0002 UPDATE tmptable SET extend_code = NULL 0.0002 UPDATE tmptable SET扩展代码= NULL

0.0153 INSERT INTO vacancies SELECT * FROM tmptable 0.0153将空位插入SELECT * FROM tmptable

However if i look in mysql itself for the new vacancy duplicated, the row it inserted everything in the update statement works except for the $banner, it inserted the exact same name as in the original vacancy (image.jpg in this example instead of image_8.jpg). 但是,如果我在mysql本身中查找重复的新空缺,则它在update语句中插入所有内容的行都可以使用,但$ banner除外,它插入的名称与原始空缺完全相同(在此示例中为image.jpg,而不是image_8 .jpg)。

Why not just do: 为什么不做:

INSERT INTO vacancies
    SELECT
    NULL, --id field
    other_columns,
    go_here,
    NULL,
    :banner, --bind this from $banner,
    now(),
    0,
    0,
    0,
    0,
    NULL
    FROM vacancies
    WHERE vacancy_id = ' . $vacid

Seems like a stretch to involve a temporary table, and a ton of queries, when you can simply do this in one. 当您可以简单地在一个临时表中执行此操作时,似乎需要一个临时表和大量查询。 Also: Look into binding variables to your query: It's much safer, and the recommend approach. 另外:研究变量绑定到查询:这是更安全的方法,也是推荐的方法。

Alternatively, you can also do this via a transaction: 另外,您也可以通过事务来执行此操作:

START TRANSACTION;
INSERT INTO vacancies
SELECT
*
FROM vacancies
WHERE vacancy_id = :vacid;
UPDATE vacancies
SET banner = :banner,
create_time = now(),
watch_counter = 0,
...
WHERE vacancy_id = :vacid --Get the inserted ID from mysql_insert_id();
COMMIT;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM