我有两个表要加入,我希望每行只加入一次。以下是示例数据:
CREATE TABLE A (id smallint, val varchar(1) );
CREATE TABLE B (id smallint, val varchar(1) );
INSERT INTO A VALUES (1, 'a'), (2, 'b'), (3, 'c'), (3, 'd');
INSERT INTO B VALUES (2, 'x'), (3, 'y'), (4, 'z'), (3, 'k');
当我们加入id时,我们获得:
mysql> SELECT * FROM A JOIN B ON A.id = B.id;
+------+------+------+------+
| id | val | id | val |
+------+------+------+------+
| 2 | b | 2 | x |
| 3 | c | 3 | y |
| 3 | d | 3 | y |
| 3 | c | 3 | k |
| 3 | d | 3 | k |
+------+------+------+------+
我想要的是:
+------+------+------+------+ +------+------+------+------+
| id | val | id | val | | id | val | id | val |
+------+------+------+------+ or +------+------+------+------+
| 2 | b | 2 | x | | 2 | b | 2 | x |
| 3 | c | 3 | y | | 3 | d | 3 | y |
| 3 | d | 3 | k | | 3 | c | 3 | k |
+------+------+------+------+ +------+------+------+------+
顺序和安排无关紧要。
有可能吗?怎么样?
根据this answer,我需要指定如何选择匹配的行。在这种情况下,我想如果已经使用了连接表的行,则需要检查子查询;或者一种与id有关的反击......但我不知道怎么写这个。
修改
澄清我希望每个具有id 3的行与连接表中的另一行映射,例如每行只映射一次(我也有兴趣知道当具有相同id的行数不同时会发生什么在两个表中):
(3, c) -> (3, y) [join only with the first row such as B.id = 3]
(3, d) -> (3, k) [the first row has been used, so map with (and only with) the second row such as B.id = 3]
但正如我所说,映射可以是任何其他顺序(例如,以相反的顺序映射行)。
答案 0 :(得分:1)
MySQL 5.6架构设置:
CREATE TABLE A (id smallint, val varchar(1) );
CREATE TABLE B (id smallint, val varchar(1) );
INSERT INTO A VALUES (1, 'a'), (2, 'b'), (3, 'c'), (3, 'd');
INSERT INTO B VALUES (2, 'x'), (3, 'y'), (4, 'z'), (3, 'k');
查询1 :
select
aa.id as aid
, aa.val as aval
, bb.id as bid
, bb.val as bval
from (
select
@row_num :=IF(@prev_value=a.id,@row_num+1,1)AS RowInGroup
, a.id
, a.val
, @prev_value := a.id
from (
SELECT id, val
FROM A
group by id, val
/* order by ?? */
) a
CROSS JOIN (
SELECT @row_num :=1, @prev_value :=''
) vars
) aa
INNER JOIN (
select
@row_num :=IF(@prev_value=b.id,@row_num+1,1)AS RowInGroup
, b.id
, b.val
, @prev_value := b.id
from (
SELECT id, val
FROM B
group by id, val
/* order by ?? */
) b
CROSS JOIN (
SELECT @row_num :=1, @prev_value :=''
) vars
) bb on aa.id = bb.id and aa.RowInGroup = bb.RowInGroup
order by
aa.id
, aa.val
<强> Results 强>:
| id | val | id | val |
|----|-----|----|-----|
| 2 | b | 2 | x |
| 3 | c | 3 | k |
| 3 | d | 3 | y |
nb :您可以通过在order by
计算序列group by id, val
的子查询中引入RowInGroup
来影响最终结果。
答案 1 :(得分:0)
最后我做到了!
SELECT T.ID_A,
T.VAL_A,
T.XXXX,
T.ID_B,
T.VAL_B,
T.YYYY
FROM (
SELECT A.id AS ID_A,
A.VAL AS VAL_A,
ROW_NUMBER() OVER (PARTITION BY A.ID, A.VAL ORDER BY A.ID, A.VAL) AS XXXX,
B.ID AS ID_B,
B.VAL AS VAL_B,
ROW_NUMBER() OVER (PARTITION BY B.ID, B.VAL ORDER BY B.ID DESC, B.VAL) AS YYYY
FROM A INNER JOIN B ON A.id = B.id) AS T
WHERE T.YYYY = 1
答案 2 :(得分:0)
感谢这个blog post:
SELECT A2.id, A2.val, B2.val FROM (
SELECT l.id, l.val, COUNT(*) AS n1 FROM A AS l JOIN A AS r ON l.id = r.id AND l.val >= r.val GROUP BY l.id, l.val
) AS A2 JOIN (
SELECT l.id, l.val, COUNT(*) AS n2 FROM B AS l JOIN B AS r ON l.id = r.id AND l.val >= r.val GROUP BY l.id, l.val
) AS B2 ON
A2.id = B2.id AND n1 = n2;
结果是:
+------+------+------+
| id | val | val |
+------+------+------+
| 2 | b | x |
| 3 | c | k |
| 3 | d | y |
+------+------+------+