I want to select features from a layer (called Road) with two fields ( A and B) with a certain condition. I want to get features with (A = MyA and B =MyB). A and B are string.

This is my code. However, I don't know why it only selects the features according to the first part (A = MyA) and ignors the AND ?? Do you know how to use AND operator in SelectbyExpression??

MyA = T[1]
MyB =T[2]

Road.selectByExpression(" \"A\" = '{}' ".format(MyA) and " \"B\" = '{}' ".format(MyB), QgsVectorLayer.SetSelection)

selectedRoad= Road.selectedFeature()
  • 1
    Use one string, with the and inside and two arguments to one .format -- Right now you're doing an and on strings. – Vince Aug 26 '19 at 2:04
  • Could you please edit my code so I understand how to do it? – Shiva Aug 26 '19 at 2:22
  • Thanks @Vince I found it should be like this :Road.selectByExpression(" \"A\" = '{}' and "\"B\"='{}' ".format(MyA,MYB),QgsVectorLayer.SetSelection) – Shiva Aug 26 '19 at 2:38

Browse other questions tagged or ask your own question.