如何在同一列上选择一些值,然后将剩余值作为 sql 中的新列
Posted
技术标签:
【中文标题】如何在同一列上选择一些值,然后将剩余值作为 sql 中的新列【英文标题】:how to select some values on the same column then remaining values as the new column in sql 【发布时间】:2017-04-27 14:18:25 【问题描述】:我有这个代码
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");
我想将性别(jkelamin)为 w 的 nama 列上的 5 个值作为女性溢出到新列中 然后将性别为 P 的剩余值作为男性放入新列中
举个例子
此为女性专栏
这是男专栏
好像
YUNIKA TRIATNADI A 与 ZIAD 结婚
YULIA 嫁给了 TUNJUNG ARIWIBOWO 等等。
我该怎么做?
【问题讨论】:
是的,只在查询@Strawberry @Strawberry 是的,就像情侣桌 @Strawberry 是的,它的男柱和女柱并排 如果两个人的结婚日期相同,那么就假定他们已经结婚了。 是否保证每个W都有一个P? 【参考方案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;
【讨论】:
大概这个数据与一个单一的登记处有关,它一次只能登记一个婚姻。 我妈才知道男女婚期居然是一样的原因,你说的对【参考方案2】: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 |
+---------------------+--------------------+
【讨论】:
以上是关于如何在同一列上选择一些值,然后将剩余值作为 sql 中的新列的主要内容,如果未能解决你的问题,请参考以下文章
使用 T-SQL 中的 OVER 子句在除一列之外的所有列上选择 DISTINCT