本文介绍了在SQL中插入多个记录,其中值是单个查询中已定义范围的所有组合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何编写sql查询

我需要类似的东西

Insert Into database.table (userID,credID,time)
values
userId = for all in (10,15,12,17,14,267,16,689,18,7659,20)
credID = for all in (1,2,3,4,5)
time = constant (forall the same)

现在的数据库结构

userID,credID,时间10,34,2013-12-1210,54,2013-12-12

userID,credID,time10,34,2013-12-1210,54,2013-12-12

所以我必须得到

userID,credID,time
10,34,2013-12-12
10,54,2013-12-12
10,1,2013-12-12
10,2,2013-12-12
10,3,2013-12-12
10,4,2013-12-12
10,5,2013-12-12
11,1,2013-12-12
11,2,2013-12-12
11,3,2013-12-12
11,4,2013-12-12
11,5,2013-12-12
....

逻辑算法必须像

范围(10,11,12,13,14,15,16,17,18,19,20)中的每个用户ID插入带有credID值(1,2,3,4,5)的新字段

for each userID in range (10,11,12,13,14,15,16,17,18,19,20)insert new fields with credID values (1,2,3,4,5)

对于单个userID,我可以使用查询创建单个credIDinsert into database.table (userID,credID,time) values (10,1,2013-12-12)但是我需要为多个用户ID插入多个credID

For single userID i can create single credID with queryinsert into database.table (userID,credID,time) values (10,1,2013-12-12)but i need to insert multiple credID for multiple userID

推荐答案

尝试这种方式:

INSERT INTO table1( userID,credID,time)
SELECT x,y,'2013-12-12'
FROM (
  SELECT 1 As x union
  SELECT 2 union
  SELECT 3 union
  SELECT 4 union
  SELECT 5
) xx
CROSS JOIN (
  SELECT 10 As y union
  SELECT 11 union
  SELECT 12 union
  SELECT 13 union
  SELECT 15 union
  SELECT 16 union
  SELECT 17 union
  SELECT 18 union
  SELECT 19 union
  SELECT 20
) yy

演示: http://www.sqlfiddle.com/#!2/8398d/1

编辑.

如果这两个数字列表是密集的,那么数字表还有另一个技巧:

EDIT.

If these two lists of numbers are dense, there is another trick with table of numbers:

CREATE TABLE numbers( x int primary key auto_increment );

INSERT INTO numbers
SELECT null FROM information_schema.columns
LIMIT 100;

CREATE TABLE   table2
  (userID int,credID int,time date);

INSERT INTO table2( userID,credID,time)
SELECT n1.x,n2.x,'2013-12-12'
FROM numbers n1
CROSS JOIN numbers n2
WHERE n1.x BETWEEN 1 AND 5
  AND n2.x BETWEEN 10 AND 20
;

演示: http://www.sqlfiddle.com/#!9/e121d/1

编辑.

数字表还有另一个技巧.
如果要将这两个列表作为逗号分隔的字符串传递,请尝试以下查询:

EDIT.

There is an another trick with the table of numbers.
If you want to pass these two lists as comma separated strings, then try this query:

CREATE TABLE numbers( x int primary key auto_increment );

INSERT INTO numbers
SELECT null FROM information_schema.columns
LIMIT 100;

CREATE TABLE   table1
  (userID int,credID int,time date);

INSERT INTO table1( userID,credID,time)
SELECT xx,yy,'2013-12-12'
FROM (
        SELECT reverse( if( locate(',',reverse(SUBSTRING_INDEX( y, ',', x ))) > 0,
                          substr( reverse(SUBSTRING_INDEX( y, ',', x )), 1, locate(',',reverse(SUBSTRING_INDEX( y, ',', x ))) -1 ),
                          reverse(SUBSTRING_INDEX( y, ',', x ))
                     )) AS xx
        FROM (  select '1,22,333,44,51,656'  y ) q
        JOIN numbers n
        ON n.x <= length( y ) - length( replace( y, ',','')) + 1
) q1
CROSS JOIN
(
        SELECT reverse( if( locate(',',reverse(SUBSTRING_INDEX( y, ',', x ))) > 0,
                          substr( reverse(SUBSTRING_INDEX( y, ',', x )), 1, locate(',',reverse(SUBSTRING_INDEX( y, ',', x ))) -1 ),
                          reverse(SUBSTRING_INDEX( y, ',', x ))
                     )) AS yy
        FROM (  select '111,222,3333,444,54,656'  y ) q
        JOIN numbers n
        ON n.x <= length( y ) - length( replace( y, ',','')) + 1
) q2
;

演示-> http://www.sqlfiddle.com/#!9/83c86/1

这篇关于在SQL中插入多个记录,其中值是单个查询中已定义范围的所有组合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-26 06:49