-1

I have a table I want to fill with data. I'm building a lookup table and I want to prime the data that will be included. The lookup table will have about 1400 rows so a standard multi insert does not seem correct. Additionally I want to prime this table without PHP if possible. This is the form of my table:

CREATE TABLE score_percentile_lookup (
    id INT(11) NOT NULL AUTO_INCREMENT,
    instance_score INT(11), 
    percentile DECIMAl(5, 2)
    PRIMARY KEY(id)
);

Basically, to start, instance score needs to be an incremented list [0 .. 1400], and percentile can be blank. Does mysql support syntax to run an insert that would basically look something like as followed:

INSERT into score_percentile_lookup (instance_score) VALUES (0), (1), ... (1400);

I could use PHP to generate this list but I want to know if its possible without it. Thank you to anyone who helps.

usumoio
  • 3,500
  • 6
  • 31
  • 57

2 Answers2

1

There is no such syntax inherent in MySQL.

You can however use a looping construct within a stored program:

DELIMITER ;;

CREATE PROCEDURE insert_ints(lbound INT, ubound INT, step INT UNSIGNED) BEGIN
  PREPARE stmt FROM '
    INSERT INTO score_percentile_lookup (instance_score) VALUES (?)
  ';

  SET @acc := lbound;
  WHILE @acc <= ubound DO
    EXECUTE stmt USING @acc;
    SET @acc := @acc + step;
  END WHILE;
  SET @acc := NULL;

  DEALLOCATE PREPARE stmt;
END;;

DELIMITER ;

And then do:

CALL insert_ints(0,1400,1);

Alternatively, if you already have a table containing such a range of numbers, you could use INSERT ... SELECT:

INSERT INTO score_percentile_lookup (instance_score)
  SELECT num FROM numbers WHERE num BETWEEN 0 AND 1400;
eggyal
  • 122,705
  • 18
  • 212
  • 237
1

Adapted from excellent answer in related question:

INSERT INTO score_percentile_lookup
(instance_score)
SELECT TOP 1401
(TWO_1.SeqValue + TWO_2.SeqValue + TWO_4.SeqValue + TWO_8.SeqValue + TWO_16.SeqValue + TWO_32.SeqValue + TWO_64.SeqValue + TWO_128.SeqValue + TWO_256.SeqValue + TWO_512.SeqValue + TWO_1024.SeqValue) SeqValue
FROM
(SELECT 0 SeqValue UNION ALL SELECT 1 SeqValue) TWO_1
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 2 SeqValue) TWO_2
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 4 SeqValue) TWO_4
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 8 SeqValue) TWO_8
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 16 SeqValue) TWO_16
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 32 SeqValue) TWO_32
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 64 SeqValue) TWO_64
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 128 SeqValue) TWO_128
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 256 SeqValue) TWO_256
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 512 SeqValue) TWO_512
CROSS JOIN (SELECT 0 SeqValue UNION ALL SELECT 1024 SeqValue) TWO_1024
ORDER BY SeqValue
Community
  • 1
  • 1
Doug Morrow
  • 1,306
  • 1
  • 10
  • 18