I recently came to this forum with the virtually the same question, but now I'm using an "AND" instead of an "OR". For the life of me I just cannot get this to work, and I don't know why. The help I received for my last issue resolved my problem perfectly. But now my query is only slightly more complex and it just won't go...so I'm wondering if a 2nd pair of eyes will help in my case.
It seems that I can do this when I hard code everything, but as soon as I try and use a variable it crashes with a very non-descript error message (e.g. Invalid Expression).
I have used 6 different expressions (listed and numbered below), and only expression2 and expression6 don't work...and these are the ones I need to work as this code is going to be transplanted into my main Python script once it's functioning.
expression1 is what works now after the help I received here...but I thought I could easily get the AND operator working because I now have the code working with an "OR" operator. That didn't happen LOL. Ideally, I would like to use the "AND" Boolean operator in this case (e.g. DateTime_2 = x and MMSI = y)...but I continue to struggle with this.
I try to keep my code very simple so it's easy for me to debug...and for the most part I'm able to do this (I'll continue to do this until I get better at Python scripting of course). Can anyone see my errors? Why can I not use expression2 or expression6? I thought expression2 was almost an exact replica of expression1, but I guess not.
I use the exact same variable and value "record11" by itself in another script and it's fine...so I'm confident this isn't (by itself) the issue.
The query I would like to use would be similar to: DateTime_2 = fme_name_1b and MMSI = record11
year_1 = "2019"
month_1 = "01"
Q2_A = "201901"
record11 = 316003484
fme_name_1b = ((year_1) + (month_1)) #fme_name_1b now = 201901
print ("fme_name_1b = " + str(fme_name_1b))
fc1 = r'F:\ArcGIS_Pro_Projects\AIS\AIS_Data\AIS_Data.gdb\TEST_AIS_Data'
#expression1 = ("DateTime_2 = '" + str(Q2_A) + "' or DateTime_2 = '" + str(Q2_B) + "' or DateTime_2 = '" + str(Q2_C) + "'") # WORKS PERFECTLY
#expression2 = ("DateTime_2 = '" + str(fme_name_1b) + "' and MMSI = '" + str(record11) + "'") # DOES NOT WORK
#expression3 = ("DateTime_2 = '201907' And MMSI = 316012834") # WORKS PERFECTLY
#expression4 = ("DateTime_2 = '" + str(Q2_D) + "'") # WORKS PERFECTLY
expression5 = ("MMSI = 316003484") # WORKS PERFECTLY
#expression6 = ("MMSI = '" + str(record11) + "'") # DOES NOT WORK
print ("Selecting records in fc1 that correspond to MMSI found in Summary Table")
SelectResult = arcpy.SelectLayerByAttribute_management((fc1), "NEW_SELECTION", (expression5))#Select records in feature class based on unique identifier (MMSI)
CountTotal = arcpy.GetCount_management((fc1))
CountResult = arcpy.GetCount_management(SelectResult)
print (str(CountResult) + " Of " + str(CountTotal) + " Records Selected")