MySQL group concatenation - creation of insert statement on temp table I have a temporary table TEST_GROUP, from where I'm working its data to fill another one.



From this table, giving the example in the picture, my expected result is to group these three rows, so I can have a result like 

INSERT INTO TABLEDATA Select * from TEST2 where (field1 = 'B') and (field2 = 'T') and (field3 != 'S' and field3 != '')

Easy to say that when the EXP = EQ only validates the VAL1, but when the EXP=NE should validate VAL1 and VAL2, even if VAL2 is empty.

Not so easy for me to get the expected result.

Can you provide some guidelines how to get the expected result?
There is no SQL that can directly perform what you have described.  Instead...

Read that data into your programming language (PHP, Java, VB, etc) and use code to construct the statement in question.  Then execute it.

Yes, a Stored Procedure could be constructed, but that might be messier.