QGIS Layers – Selecting Features in One Layer Based on Mutual Field with Another Layer

attribute-tablefields-attributeslayersqgisselect-by-attribute

I have two layers in QGIS, each one with a field called "Name". I need to select the features in 'layer2' whose name exists in 'layer1' (totally or partially).

For example, in the images below, I want the following features in layer2 (on the right) to be selected: 197, 198, 200, 201, 203, 204, and 206.

enter image description here

How can I do that?

Best Answer

Use "Select by expression" with this expresstion on layer 1:

array_contains( 
    aggregate( 
        'points1',  -- change this to the name of your layer1
        'array_agg',
        name1  -- change this to the name of the name field in layer1
    ),
    name2  -- change this to the name of the name field in layer2
)

Explanation: The function aggregate() creates an array of all values of the field "name1" in the layer named points1. array_contains() checks if the value (content, string) of each feature on layer 2 in the field "name2" is contained in this array and in this case returns true, otherwise false.

enter image description here