Red > Green > Refactor > Red

cycle is based on desire

順列テーブル

PostgreSQL 9.4.1

こんなテーブルから

products

      name      
----------------
 cpu_heat
 mouse_cat
 keyboard_punch

以下順列テーブルを出す

      name      |      name      
----------------+----------------
 cpu_heat       | cpu_heat
 cpu_heat       | mouse_cat
 cpu_heat       | keyboard_punch
 mouse_cat      | cpu_heat
 mouse_cat      | mouse_cat
 mouse_cat      | keyboard_punch
 keyboard_punch | cpu_heat
 keyboard_punch | mouse_cat
 keyboard_punch | keyboard_punch

前提

create table products (
  name  text,
);

データ投入

INSERT INTO products
  (name)
  VALUES
  ('cpu_heat');

INSERT INTO products
  (name)
  VALUES
  ('mouse_cat');

INSERT INTO products
  (name)
  VALUES
  ('keyboard_punch');
select * from products;

      name      
----------------
 cpu_heat
 mouse_cat
 keyboard_punch

操作

SELECT PA.name, PB.name
FROM
  products as PA, products as PB;

もしくは

SELECT PA.name, PB.name
FROM
  products as PA  CROSS JOIN products as PB;

結果

      name      |      name      
----------------+----------------
 cpu_heat       | cpu_heat
 cpu_heat       | mouse_cat
 cpu_heat       | keyboard_punch
 mouse_cat      | cpu_heat
 mouse_cat      | mouse_cat
 mouse_cat      | keyboard_punch
 keyboard_punch | cpu_heat
 keyboard_punch | mouse_cat
 keyboard_punch | keyboard_punch

www.sql-reference.com