Mysql
 sql >> Teknologi Basis Data >  >> RDS >> Mysql

Cara memilih satu baris 100 juta x

Ubah sedikit solusi Bruno

SELECT (SELECT COALESCE(max(id),0)+1 FROM table1), 
        @rownum:[email protected]+1 new_id 
FROM 
(SELECT @rownum:=0) r, 
(SELECT 1 UNION ALL SELECT 2) t1,
(SELECT 1 UNION ALL SELECT 2) t2,
(SELECT 1 UNION ALL SELECT 2) t3,
(SELECT 1 UNION ALL SELECT 2) t4,
(SELECT 1 UNION ALL SELECT 2) t5,
(SELECT 1 UNION ALL SELECT 2) t6,
(SELECT 1 UNION ALL SELECT 2) t7
LIMIT 100

Atau versi lain tanpa variabel

SELECT (SELECT Coalesce(MAX(id), 0) + 1
        FROM   table1),
       t1.n * 10 + t2.n + 1 AS new_id
FROM   (SELECT 0 AS n UNION ALL
        SELECT 1      UNION ALL
        SELECT 2      UNION ALL
        SELECT 3      UNION ALL
        SELECT 4      UNION ALL
        SELECT 5      UNION ALL
        SELECT 6      UNION ALL
        SELECT 7      UNION ALL
        SELECT 8      UNION ALL
        SELECT 9) t1,
       (SELECT 0 AS n UNION ALL
        SELECT 1      UNION ALL
        SELECT 2      UNION ALL
        SELECT 3      UNION ALL
        SELECT 4      UNION ALL
        SELECT 5      UNION ALL
        SELECT 6      UNION ALL
        SELECT 7      UNION ALL
        SELECT 8      UNION ALL
        SELECT 9) t2
ORDER  BY new_id  


  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. File kosong yang dihasilkan dari menjalankan `mysqldump` menggunakan PHP

  2. Dapatkan durasi sebagai perbedaan cap waktu untuk perubahan lokasi berulang

  3. Permintaan MySQL lambat

  4. Masalah aksesor toko:Tidak dapat membaca nilai objek json yang sudah tersimpan lama (hash) dalam database mysql di Rails 4

  5. Mengapa kueri UNION sangat lambat di MySQL?