mysqlautomationmysql-variablesmysql-function

Query build automation


I have the following query

select count(t1.guid)
from table t1
where t1.id=X;

X is a result-set from this query

select ID 
from table t2 
where t2.flags=65537;

The above query returns 84 results, all of INT datatype.

id is primary key in t2 table, and foreign key in t1 table;

guid is primary key in t1 table, and doesn't exist anywhere else.

Object O1 has a unique identifier among the table that declares all objects and their properties (t2) GUID in table t1 assigns unique identification to every instance of object O1 called by upper layers. I want to see the number of duplicates every object that fulfills conditions in the second query.

I suppose I should go about declaring a variable and a function that uses said variable but got no clue where to start or how to go about it.

I solved the problem once with hand-hacking 84 times, but looking for a more elegant and more adaptive solution to this;


Solution

  • After a whole day spent, figured it out

    Simply link the two posted queries together, but change the "=" operator to "in"

    select count(t1.guid)
    from table t1
    where t1.id in
    (select t2.ID 
    from table t2 
    where t2.flags=65537);
    

    hand-hacking session avoided!