简体   繁体   中英

How to select unique values from one column based on a value in another column?

The structure of the table is that a value in the child_id column can be duplicated but has a different associated parent_id for each duplicate. Say child_id 1 has four parents, then it will have 3 copies of itself in the child_id column, but each duplicate is associated with each of the 3 different parent_id .

Given those conditions, what I'm trying to achieve is to sort by parent_id and remove the unwanted child_id duplicates by picking only one based on the last or closest parent (level of that parent would be -1 the level of the child). Is that possible?

I tried to create a fiddle on sqlfiddle but I keep getting a create script error so I guess I'll just paste the code here:

CREATE TABLE `parent_child_columns` (`id` int auto_increment,
                                     `child_id` int,
                                       `parent_id` int,
                                     `level` int,
                                     PRIMARY KEY (`id`))

INSERT INTO `parent_child_columns` (`child_id`, `parent_id`, `level`)
VALUES (1, 0, 1), (2, 0, 1),
       (3, 1, 2), (4, 1, 2),
       (5, 3, 3), (5, 1, 3), (6, 3, 3), (6, 1, 3)



SELECT pc.child_id, pc.level
FROM parent_child_columns pc
ORDER BY pc.parent_id

With this query, the duplicates still get picked up so a column is already displayed before another one when it should come AFTER that other one (since its level 1 parent is at the top when sorted ASC). (A parent column can have many child columns.) I can include screencaps of the actual data in my database if necessary.

I'm building a dynamic table header here where you can add an indefinite number of nested columns. There's a bit more stuff going on with how I arrange the rows and columns so if my explanation here is still a bit vague and you can't get a clear picture, please let me know. But a rundown of what I do is basically I try to build row by row and arrange the columns in each child level (or row) based on the order of the parent columns right above them.

What I'm getting: 在此处输入图片说明 在此处输入图片说明

Expected output is that in the actual table, 1843 should be under 1842 , as seen in the database that its parents are 1839 and 1842 . I'm only assuming that it's already under 1841 because it gets picked up earlier, but maybe that's not actually the case? Any better ideas as to what might be causing that?

PS In the example above I tried to split 1841 so it should have the 2 New Columns under it.

If you want the parent with "-1" the level of the child, then use join :

SELECT pc.*, pcp.id
FROM parent_child_columns pc LEFT JOIN
     parent_child_columns pcp
     ON pc.parent_id = pcp.child_id AND pcp.level = pc.level - 1
ORDER BY pc.parent_id;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM