简体   繁体   English

仅为一列插入不同的值

[英]Insert distinct values only for one column

Say, I have a table with five columns col1 , col2 , col3 , col4 and user_id .比如说,我有一个包含五列col1col2col3col4user_id Now, I have an array of user_id values, say a thousand.现在,我有一组user_id值,比如说一千。 I want to insert thousand of records where only distinct column value is user_id .我想插入数千条记录,其中只有不同的列值是user_id If there is a simplier way rather than make a thousand of ('col1value','col2value','col3value','col4value',someUserId) and concatenate them in single insert into tbl (col1,col2,col3,col4,user_id) values query?如果有更简单的方法而不是制作一千个 ('col1value','col2value','col3value','col4value',someUserId) 并将它们连接insert into tbl (col1,col2,col3,col4,user_id) values查询?

Update: I guess it needs some clarification更新:我想它需要一些澄清

So here's simple example.所以这是一个简单的例子。 Let's say I have an events table with fields event and user_id .假设我有一个带有字段eventuser_idevents表。 Some call event occurs for users with id 1, 2, 5, 101, 233, 422 and 1000. So I need to insert 7 records into table so it should look like id 为 1、2、5、101、233、422 和 1000 的用户会发生一些call事件。所以我需要在表中插入 7 条记录,所以它应该看起来像

+-------+---------+
| event | user_id |
|-------|---------|
|  call |       1 |
|  call |       2 |
|  call |       5 |
|  call |     101 |
|  call |     233 |
|  call |     422 |
|  call |    1000 |
+-------+---------+

I want to do it as efficiently database-wise as possible.我想尽可能高效地在数据库方面进行操作。 So far, I think I have to make such SQL query:到目前为止,我认为我必须进行这样的 SQL 查询:

insert into events (event,user_id) values ('call',1),('call',2),('call',5),('call',101),('call',233),('call',422),('call',1000);

then perform a single query然后执行单个查询

But maybe there is some more simple and efficient way?但也许有一些更简单有效的方法? Maybe something with SQL parameters or such?也许有 SQL 参数之类的东西?

I understand your question that you have a high number of different user_id values that you want to insert, but for each new record you want the same values for col1 , col2 , col3 and col4 .我理解您的问题,您有大量不同的user_id值要插入,但对于每条新记录,您都希望col1col2col3col4具有相同的值。

The easiest way to achieve this would be to set a DEFAULT value for each of the columns:实现这一点的最简单方法是为每一列设置一个DEFAULT值:

ALTER TABLE mytable CHANGE `col1` `col1` INTEGER NOT NULL DEFAULT 1234

This sets the default value for col1 to 1234 .这将col1的默认值设置为1234 I assumed the column to be of data type INTEGER , you need to change it accordingly.我假设该列的数据类型为INTEGER ,您需要相应地更改它。

If changing the table is not an option for you, then you could still build an insert statement that would insert all records in a single transaction:如果更改表不适合您,那么您仍然可以构建一个插入语句,在单个事务中插入所有记录:

$user_ids = array(5,6, 7, 8, 9, 10); // these are the user ids you want to insert
$default_vals = array(1, 2, 3, 4); // These are the default values used for col1, col2, col3 and col4

$con = new mysqli('mydomain', 'myuser', 'mypw', 'mydb');

$rows = array();
foreach ($user_ids as $id)
    $rows[] = "(".implode(',', $default_vals).",$id)";
$sql = "INSERT INTO mytbl (col1, col2, col3, col4, id) VALUES " . implode(',', $rows) . ";";

$con->query($sql);

Or, if for academic reason you'd rather write a single INSERT statement to do the whole job, you could write:或者,如果出于学术原因,您宁愿编写一个INSERT语句来完成整个工作,您可以这样写:

INSERT INTO mytbl (col1, col2, col3, col4, id)
SELECT * FROM 
  (SELECT 1, 2, 3, 4) AS DEFAULT_VALS,
  (SELECT 5
   UNION SELECT 6
   UNION SELECT 7
   UNION SELECT 8
   UNION SELECT 9) AS USER_IDS

However, the last approach really isn't very nice.但是,最后一种方法确实不是很好。 Neither in terms of readability nor of performance.无论是在可读性还是性能方面。


Edit 编辑
I made a quick benchmark for you: 我为你做了一个快速的基准测试:

  • Inserting 10k different records like in my PHP sample took 1.5086660385132 seconds on my webserver.像在我的 PHP 示例中那样插入 10k 条不同的记录在我的网络服务器上花费了1.5086660385132 秒
  • The SELECT INTO approach with 10k UNION s took 3.3481941223145 seconds .带有 10k UNIONSELECT INTO方法花费了3.3481941223145 秒

Easy choice, though.不过选择很简单。

Use :用 :

INSERT INTO tbl (col1,col2,col3,col4,user_id)
VALUES 
  (1,2,3,4,1000),
  (2,3,4,5,1000),
  (6,7,8,9,1000),
  (1,2,3,4,1234),
  (4,3,2,1,1235);

Create unique index on user_id field.user_id字段上创建唯一索引。 use INSERT IGNORE or INSERT ... ON DUPLICATE KEY UPDATE for ignoring duplicate insertion of user_id .使用INSERT IGNOREINSERT ... ON DUPLICATE KEY UPDATE忽略user_id重复插入。

For details, kindly refer MySQL documentation.有关详细信息,请参阅 MySQL 文档。

http://dev.mysql.com/doc/refman/5.6/en/insert.html http://dev.mysql.com/doc/refman/5.6/en/insert.html

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

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