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

MYSQL:Menghindari produk cartesian dari pengulangan catatan saat bergabung sendiri

select a_numbered.id, a_numbered.identifier, b_numbered.id from 
(
select a.*,
       case 
          when @identifier = a.identifier then @rownum := @rownum + 1
          else @rownum := 1
       end as rn,
       @identifier := a.identifier
  from a
  join (select @rownum := 0, @identifier := null) r
order by a.identifier

) a_numbered join (
select b.*,
       case 
          when @identifier = b.identifier then @rownum := @rownum + 1
          else @rownum := 1
       end as rn,
       @identifier := b.identifier
  from b
  join (select @rownum := 0, @identifier := null) r
order by b.identifier

) b_numbered 
on a_numbered.rn=b_numbered.rn and a_numbered.identifier=b_numbered.identifier



  1. Database
  2.   
  3. Mysql
  4.   
  5. Oracle
  6.   
  7. Sqlserver
  8.   
  9. PostgreSQL
  10.   
  11. Access
  12.   
  13. SQLite
  14.   
  15. MariaDB
  1. MySQL Workbench - Cara menyinkronkan Diagram EER

  2. varchar(255) vs tinytext/tinyblob dan varchar(65535) vs gumpalan/teks

  3. MySQL FULL JOIN tidak berfungsi tetapi RIGHT dan LEFT join berfungsi

  4. Tes Ekspresi Regex MySql

  5. Praktik terbaik untuk panjang kolom SQL varchar