Skip to content
Advertisement

SELECT with a variable

I have 3 tables :

  1. t_object : With all the classes
  2. t_operation : With all the operations, link with t_object.Object_ID = t_operation.Object_ID
  3. t_operationtag : With all the tag value for each operation, link with t_operation.OperationID = t_operationtag.ElementID

I try to get an list for each of my operation (1) with all the operation (2) which calls the operation (1) :

So I write this request :

Here sample data to test the query : https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=3711bb22e52c4528898ece82833042ce

But my problem is the query return only the “CallsBy” for the operation “class2Operation1”

I would like to get all the CallsBy for all the operation of the select Class. The result for the class 1 would be :

  • Name Operation | CallsBy
  • class1Operation1 | class2Operation2, class3Operation1
  • class1Operation2 | class2Operation3
  • class1Operation3 | class2Operation3

Class 2

  • Name Operation | CallsBy
  • class2Operation1 | class1Operation1, class3Operation1,class3Operation3
  • class2Operation2 | class1Operation1
  • class2Operation3 | class1Operation1

Class 3

  • Name Operation | CallsBy
  • class3Operation1 | class1Operation2, class3Operation1
  • class3Operation2 | class1Operation3
  • class3Operation3 | class2Operation1

My only variable to select a class I could use is WHERE myClass.Object_ID = ‘1’.

This request works well but I would like to change automaticaly “MSA_Se_SECU_ComputeHMAC” by using a variable. I try this :

But this don’t work because He didn’t know “op2.Name”, and I have no idea if it’s possible to do ? maybe with a variable ? Hope its enough clear. Here some sample data to test the query : https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=3711bb22e52c4528898ece82833042ce

Advertisement

Answer

After some extensive discusion with OP this is the solution:

Here is the DEMO of the solution

And here are some notes to his question and code in question:

First query or how you call it request is not ok. It returns error that is telling you that you can not group by OperationID if it is not in select statement (or in agregate function in select statement).

If you replace, in that first query, this line:

with this line:

The code will work but the results maybe will not be the same…

Second query has more than one problem. First one is the same as in the first query(group by).

Further more, if you see up in the code, the line that is commented is not logical there. It is not a join, join condition and I do not know what you wanted to do there…

Also, in one part of the code your alias is allClass and it should be allClass2.

If you change all that you will get this:

This works. But the results that it gives is another story…

User contributions licensed under: CC BY-SA
4 People found this is helpful
Advertisement