如何在同一列上选择一些值,然后将剩余值作为sql中的新列

时间:2017-04-27 14:18:26

标签: mysql sql

我有这个代码

insert into tblPernikahan values(1,"YUNIKA TRIRATNADI A","W","1981-11-29 23:11:10","YOGYAKARTA","2008-09-11 12:09:41"),
  (2,"YULIA","W","1976-12-05 19:12:21","SEMARANG","2011-10-23 05:10:45"),
  (3,"YOLA AZERTI SARI","W","1983-06-01 03:06:40","MAGELANG","2011-06-08 17:06:28"),
  (4,"YETI SULIANA","W","1986-04-18 04:04:50","YOGYAKARTA","2005-01-14 21:01:04"),
  (5,"YETI KURNIATI P","W","1976-02-18 21:02:18","MUNTILAN","2008-11-19 23:11:50"),
  (6,"ZIAD","P","1981-07-08 17:07:06","YOGYAKARTA","2008-09-11 12:09:41"),
  (7,"TUNJUNG ARIWIBOWO","P","1978-10-25 07:10:39","YOGYAKARTA","2011-10-23 05:10:45"),
  (8,"SUGIMAN","P","1976-09-28 01:09:38","MUNTILAN","2011-06-08 17:06:28"),
  (9,"SIGIT SUTOPO","P","1976-06-22 00:06:50","YOGYAKARTA","2005-01-14 21:01:04"),
  (10,"RICKY PERMANADJAYA","P","1989-06-09 07:06:23","MAGELANG","2008-11-19 23:11:50");

weddingstable 我想把性别(jkelamin)所在的nama列上的5个值作为女性填入新列 然后将性别为P的剩余值作为男性进入新列

示例

female column这是女性专栏

male column这是男性专栏

喜欢

YUNIKA TRIATNADI A与ZIAD结婚

YULIA与TUNJUNG ARIWIBOWO结婚等等。

我该怎么做?

2 个答案:

答案 0 :(得分:1)

drop table if exists weddings;

CREATE TABLE weddings
(id INT NOT NULL AUTO_INCREMENT PRIMARY KEY
,name VARCHAR(30) NOT NULL 
,gender CHAR(1) NOT NULL 
,dob DATETIME NOT NULL 
,birthplace VARCHAR(20) NOT NULL
,wedding_date DATE NOT NULL
);

insert into weddings values
(1,"YUNIKA TRIRATNADI A","W","1981-11-29 23:11:10","YOGYAKARTA","2008-09-11 12:09:41"),
  (2,"YULIA","W","1976-12-05 19:12:21","SEMARANG","2011-10-23 05:10:45"),
  (3,"YOLA AZERTI SARI","W","1983-06-01 03:06:40","MAGELANG","2011-06-08 17:06:28"),
  (4,"YETI SULIANA","W","1986-04-18 04:04:50","YOGYAKARTA","2005-01-14 21:01:04"),
  (5,"YETI KURNIATI P","W","1976-02-18 21:02:18","MUNTILAN","2008-11-19 23:11:50"),
  (6,"ZIAD","P","1981-07-08 17:07:06","YOGYAKARTA","2008-09-11 12:09:41"),
  (7,"TUNJUNG ARIWIBOWO","P","1978-10-25 07:10:39","YOGYAKARTA","2011-10-23 05:10:45"),
  (8,"SUGIMAN","P","1976-09-28 01:09:38","MUNTILAN","2011-06-08 17:06:28"),
  (9,"SIGIT SUTOPO","P","1976-06-22 00:06:50","YOGYAKARTA","2005-01-14 21:01:04"),
  (10,"RICKY PERMANADJAYA","P","1989-06-09 07:06:23","MAGELANG","2008-11-19 23:11:50");


SELECT w.name w
     , p.name p 
  FROM weddings w 
  JOIN weddings p 
    ON p.wedding_date = w.wedding_date 
   AND p.gender = 'p' 
 WHERE w.gender = 'w';
+---------------------+--------------------+
| w                   | p                  |
+---------------------+--------------------+
| YUNIKA TRIRATNADI A | ZIAD               |
| YULIA               | TUNJUNG ARIWIBOWO  |
| YOLA AZERTI SARI    | SUGIMAN            |
| YETI SULIANA        | SIGIT SUTOPO       |
| YETI KURNIATI P     | RICKY PERMANADJAYA |
+---------------------+--------------------+

答案 1 :(得分:1)

我不太明白 - 是" tglnikah"结婚日期和确定谁是谁结婚的唯一方法?可能有多对夫妻结婚日期相同...... 否则你可以做类似

的事情
SELECT t1.nama, t2.nama, t1.tglnikah FROM tblPernikahan t1, tblPernikahan t2 WHERE t1.jkelamin='W' AND t2.jkelamin='P' AND t1.tglnikah=t2.tglnikah AND t1.nama!=t2.nama;