Maison  >  Questions et réponses  >  le corps du texte

Générer des numéros uniques à 13 chiffres : méthodes et techniques dans MySQL

Salut à tous, J'ai essayé toute la journée de générer 13 numéros uniques pour les produits existants dans ma base de données. Chaque produit doit avoir un code barre EAN13. J'ai essayé de nombreux exemples sur Internet mais aucun ne m'a donné de bons résultats. Quelqu'un a-t-il une solution à ce problème ?

J'ai essayé mais rien n'a fonctionné

select cast(  (@n := (13*@n + 100) % 899999999981)+1e12 as char(15)) as num
from   (select @n := floor(rand() * 10e14) ) init,
       (select 1 union select 2) m01,
       (select 1 union select 2) m02,
       (select 1 union select 2) m03,
       (select 1 union select 2) m04,
       (select 1 union select 2) m05,
       (select 1 union select 2) m06,
       (select 1 union select 2) m07,
       (select 1 union select 2) m08,
       (select 1 union select 2) m09,
       (select 1 union select 2) m10,
       (select 1 union select 2) m11,
       (select 1 union select 2) m12,
       (select 1 union select 2) m13,
       (select 1 union select 2) m14,
       (select 1 union select 2) m15,
       (select 1 union select 2) m16,
       (select 1 union select 2) m17,
       (select 1 union select 2) m18,
       (select 1 union select 2) m19,
       (select 1 union select 2) m20,
       (select 1 union select 2) m21,
       (select 1 union select 2) m22
limit 5;

Comment puis-je maintenant mettre à jour les 12 000 lignes d'une table existante en utilisant le code ci-dessus. J'essaie UPDATE mais j'obtiens une erreur lorsque j'essaie de mélanger UPDATE et CAST

La structure du tableau des produits est :

productid INT(11)
productName Varchar(225)
barcode INT(13)

P粉135799949P粉135799949408 Il y a quelques jours479

répondre à tous(1)je répondrai

  • P粉739886290

    P粉7398862902023-09-08 09:59:56

    Mettre à jour le code-barres. Créez une nouvelle table, puis insérez les valeurs dans la nouvelle table, puis utilisez la nouvelle table pour mettre à jour la table existante à l'aide de la mise à jour.

    Créer une requête de table :-

    CREATE TABLE unique_numbers (
      id INT NOT NULL AUTO_INCREMENT PRIMARY KEY,
      barcode VARCHAR(13) NOT NULL,
      UNIQUE KEY barcode (barcode)
    );

    Valeur à insérer :-

    INSERT INTO unique_numbers (barcode) 
    SELECT CAST(  (@n := (13*@n + 100) % 899999999981)+1e12 as char(15)) as num
    FROM 
      (SELECT @n := floor(rand() * 10e14) ) init,
      (SELECT 1 UNION SELECT 2) m01,
      (SELECT 1 UNION SELECT 2) m02,
      (SELECT 1 UNION SELECT 2) m03,
      (SELECT 1 UNION SELECT 2) m04,
      (SELECT 1 UNION SELECT 2) m05,
      (SELECT 1 UNION SELECT 2) m06,
      (SELECT 1 UNION SELECT 2) m07,
      (SELECT 1 UNION SELECT 2) m08,
      (SELECT 1 UNION SELECT 2) m09,
      (SELECT 1 UNION SELECT 2) m10,
      (SELECT 1 UNION SELECT 2) m11,
      (SELECT 1 UNION SELECT 2) m12,
      (SELECT 1 UNION SELECT 2) m13,
      (SELECT 1 UNION SELECT 2) m14,
      (SELECT 1 UNION SELECT 2) m15,
      (SELECT 1 UNION SELECT 2) m16,
      (SELECT 1 UNION SELECT 2) m17,
      (SELECT 1 UNION SELECT 2) m18,
      (SELECT 1 UNION SELECT 2) m19,
      (SELECT 1 UNION SELECT 2) m20,
      (SELECT 1 UNION SELECT 2) m21,
      (SELECT 1 UNION SELECT 2) m22
    LIMIT 12000;

    Requête de mise à jour :-

    UPDATE product a
    JOIN unique_numbers b ON a.productid = b.id
    SET a.barcode = b.barcode;

    répondre
    0
  • Annulerrépondre