Skip to content
Advertisement

How can I use IF and CONCAT within SELECT

I have this Adjacency List Model table

Table:

Output:

So I have the following query that duplicates a Site 1 e.g. and its children.
In this case, the children are Paper with parent_id = 2 and ms1 with parent_id = 3

Output:

As you can see Site 1 and its children got duplicated with a new unique id.
However for the duplicated Site title I want to have a prefix text Copy of for the DUPLICATED Site 1 title
I only want that prefix for a Site/parent_id = 1

So that the duplicated nodes should look like this:

I have tried to implement the IF and CONCAT in the query but for some reason, it doesn’t work, I don’t get any errors but the output stays the same.

If the title contains the text Site then I want to contact the prefix and the site title otherwise no concat.

Any ideas?

Any help is appreciated!!!

Advertisement

Answer

This solution shows how to insert a copy of a subtree and re-identify the descendants.

Note that starting MySQL 8 setting user variables within expressions is deprecated and will be removed in a future release.

The following fiddle shows the results of each CTE – db<>fiddle

User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement