[Qgis-user] query of a layer from another layer in select by expression

Yann Voté ygversil at lilo.org
Fri May 29 08:07:09 PDT 2020


First, I think you can replace layer ids with layer names from your QGIS 
project legend :

'BATIMENT_TERRAIN_ROLE_MARS_2020_46364fcb_8628_404d_9ca6_0ab713170ceb' 
-> 'BATIMENT TERRAIL ROLE MARS 2020' (if that is the name of the layer 
in your QGIS project legend)

However, this is risky, because the expression will fail in case you 
change the layer name in the legend.

Second, since you want to compare multiple fields, you can compute the 
related feature (with get_feature) only once and then store it into a 
variable 'feat', thanks to the function with_variable(). You can then 
reference this variable with @feat. You can also use array comparison 
instead of a long string of AND. Something like this :

with_variable(
     'feat', 
get_feature('BATIMENT_TERRAIN_ROLE_MARS_2020_46364fcb_8628_404d_9ca6_0ab713170ceb', 
'Matricule', "Matricule"),
     array(attribute(@feat, 'Aqueduc'), attribute(@feat, 'Sanitaire'), 
attribute(@feat, 'Batiment')) = array('O', 'O', 'O')
)

Le 29/05/2020 à 15:02, Patrick St-Hilaire a écrit :
> Hi Yann
> 
> Thank you very much, everything works fine, but is it possible to 
> simplify the query when we want to involve several fields? Here is an 
> example that works well where I replicate the request to achieve my 
> ends, but I can't simplify it. Perhaps it is not possible, but if so it 
> would be good. Thank you
> 
> attribute 
> (get_feature('BATIMENT_TERRAIN_ROLE_MARS_2020_46364fcb_8628_404d_9ca6_0ab713170ceb', 
> 'Matricule', "Matricule"), 'Aqueduc')= 'O' AND attribute 
> (get_feature('BATIMENT_TERRAIN_ROLE_MARS_2020_46364fcb_8628_404d_9ca6_0ab713170ceb', 
> 'Matricule', "Matricule"), 'Sanitaire')= 'O' AND attribute 
> (get_feature('BATIMENT_TERRAIN_ROLE_MARS_2020_46364fcb_8628_404d_9ca6_0ab713170ceb', 
> 'Matricule', "Matricule"), 'Batiment')= 'O'
> 
> Le jeu. 28 mai 2020 à 16:25, Yann Voté <ygversil at lilo.org 
> <mailto:ygversil at lilo.org>> a écrit :
> 
>     Hi,
> 
>     You can try the direct but implicit represent_value() function, if the
>     filtering is by the referenced column, like this:
> 
>     represent_value("referencing_field") =
>     'filter_value_on_referenced_layer'
> 
>     Or you can try the more verbose and more explicit
>     attribute()/get_feature() functions, if you want to filter by another
>     column:
> 
>     attribute(get_feature('referenced_layer_name', 'referenced_field',
>     "referencing_field"), 'filter_field') = 'filter_value'
> 
>     And be careful about quotes: 'foo' (single quotes) is a string (but may
>     designates a field in other layer in some functiones), while "foo"
>     (double quotes) designates a field in the active layer.
> 
>     Hope this helps!
> 
>     Yann
> 
>     Le 28/05/2020 à 19:11, Patrick St-Hilaire a écrit :
>      > Hi !
>      >
>      > Could someone tell me how to query a layer from fields of another
>     layer
>      > using select by expression? I have already created a relationship
>      > between my two layers in projects.
>      >
>      > Thank you
>      >
>      > Patrick
>      >
>      > _______________________________________________
>      > Qgis-user mailing list
>      > Qgis-user at lists.osgeo.org <mailto:Qgis-user at lists.osgeo.org>
>      > List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>      > Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
>      >
>     _______________________________________________
>     Qgis-user mailing list
>     Qgis-user at lists.osgeo.org <mailto:Qgis-user at lists.osgeo.org>
>     List info: https://lists.osgeo.org/mailman/listinfo/qgis-user
>     Unsubscribe: https://lists.osgeo.org/mailman/listinfo/qgis-user
> 


More information about the Qgis-user mailing list