Reputation: 3033
CREATE TABLE IF NOT EXISTS `userinfo` (
`user_id` int(6) unsigned NOT NULL,
`user_code` varchar(20),
`party1_id` varchar(200),
`party2_id` varchar(200),
PRIMARY KEY (`user_id`)
) DEFAULT CHARSET=utf8;
INSERT INTO `userinfo` (`user_id`, `user_code`, `party1_id`, `party2_id`) VALUES
('1', '05B29E57', '1', ''),
('2', '05B29E58', NULL, '1'),
('3', '05B29E59', '2', ''),
('4', '05B29E60', NULL, '2'),
('5', '05B29E61', '3',''),
('6', '05B29E62', NULL, '3'),
('7', '05B29E63', NULL, '4'),
('8', '05B29E64', NULL, '5'),
('9', '05B29E65', NULL, '6'),
('10', '05B29E66', NULL, '7');
+---------+-----------+-----------+-----------+
| user_id | user_code | party1_id | party2_id |
+---------+-----------+-----------+-----------+
| 1 | 05B29E57 | 1 | |
| 2 | 05B29E58 | NULL | 1 |
| 3 | 05B29E59 | 2 | |
| 4 | 05B29E60 | NULL | 2 |
| 5 | 05B29E61 | 3 | |
| 6 | 05B29E62 | NULL | 3 |
| 7 | 05B29E63 | NULL | 4 |
| 8 | 05B29E64 | NULL | 5 |
| 9 | 05B29E65 | NULL | 6 |
| 10 | 05B29E66 | NULL | 7 |
+---------+-----------+-----------+-----------+
So i have above table as userinfo
. Due to some reason i have to find the duplicate records for party1_id & party2_id.
I have tried below:
SELECT
*
FROM userinfo u1
JOIN userinfo u2
ON u1.party1_id = u2.party2_id
WHERE IFNULL(u1.party1_id, 0) > 0
AND IFNULL(u1.party2_id, '') = ''
Fiddle: http://sqlfiddle.com/#!9/725eff/1
But it didn't give result in a way i want, it just returns 3 record. Is there a way to archive my expected output with sorting same as in my expected output.
Expected Output:
+---------+-----------+-----------+-----------+
| user_id | user_code | party1_id | party2_id |
+---------+-----------+-----------+-----------+
| 1 | 05B29E57 | 1 | |
| 2 | 05B29E58 | NULL | 1 |
| 3 | 05B29E59 | 2 | |
| 4 | 05B29E60 | NULL | 2 |
| 5 | 05B29E61 | 3 | |
| 6 | 05B29E62 | NULL | 3 |
+---------+-----------+-----------+-----------+
Upvotes: 0
Views: 61
Reputation: 133390
You could obtain the expected result using union
select t1.*
from userinfo t1
inner join (
select a.party1_id apid, b.party2_id bpid
FROM userinfo a
JOIN userinfo b
ON a.party1_id = b.party2_id
) t2 on t2.apid = t1.party1_id
UNION
select t1.*
from userinfo t1
inner join (
select a.party1_id apid, b.party2_id bpid
FROM userinfo a
JOIN userinfo b
ON a.party1_id = b.party2_id
) t2 on t2.apid = t1.party2_id
order by user_id
http://sqlfiddle.com/#!9/725eff/17
for performance be sure you have al least thiese indexes
idx1 table userinfo columns (party1_id, party2_id)
idx2 table userinfo columns (party2_id, party1_id)
Upvotes: 1
Reputation: 132
You can give a try following.
select * from userinfo where
party1_id in (
SELECT
u1.party1_id
FROM userinfo u1
JOIN userinfo u2
ON u1.party1_id = u2.party2_id
WHERE IsNULL(u1.party1_id, 0) > 0
AND IsNULL(u1.party2_id, '') = '') or party2_id in ( SELECT
u1.party1_id
FROM userinfo u1
JOIN userinfo u2
ON u1.party1_id = u2.party2_id
WHERE IsNULL(u1.party1_id, 0) > 0
AND IsNULL(u1.party2_id, '') = '')
Upvotes: 1
Reputation: 1109
Try with below query:
SELECT
*
FROM userinfo u1
where
u1.party1_id in (select distinct u2.party2_id from userinfo u2)
or
u1.party2_id in (select distinct u3.party1_id from userinfo u3)
Below is the output I get in your fiddle for above query:(order by can be applied to any column)
user_id user_code party1_id party2_id
1 05B29E57 1
2 05B29E58 (null) 1
3 05B29E59 2
4 05B29E60 (null) 2
5 05B29E61 3
6 05B29E62 (null) 3
Upvotes: 0