Skip to main content

How to describe SQL IN statement using Gurobi constraints

Answered

Comments

3 comments

  • Riley Clement
    Gurobi Staff Gurobi Staff

    Hi Yifeng,

    I'm not sure what this question means, there is no relation between Gurobi and SQL.  Is there more context you can provide?

    - Riley

    0
  • Yifeng Mao
    Curious
    Gurobi-versary
    Conversationalist

    Hi Riley,

        Thanks for your prompt reply.

        I think evaluating the SQL queries in a database could be regarded as solving constraint satisfaction problem. The entry values in the database are set up as unknown variables, and the condition in the SQL query and the number of rows satisfying the given condition are considered as constraints. 

        To setup a toy example, the database consists of 5 rows and 3 columns (EDUCATION, GENDER, ID), thus, a total of 15 unknown variables. If the SQL query is SELECT COUNT(*) WHERE EDUCATION IN 10,11,12 AND GENDER=1, and if the answer to the SQL query is 3, then it means that 3 of 5 rows satisfying the constraints in WHERE conditions of the query.

        How could you describe the constraints in the Gurobi?

    Best

    Yifeng

    0
  • David Torres Sanchez
    Gurobi Staff Gurobi Staff

    Hi Yifeng,

    Do you mean something like this: In Python:

    import gurobipy as gp

    education = list(range(5))
    gender = list(range(5))
    idx = list(range(5))

    with gp.Model() as m:
        x = m.addVars(education, gender, idx, name="x")
      m.addConstr(gp.quicksum(x.sum(e, 1, "*") for e in [1, 2, 3]) >= 1)

    Cheers, 
    David

    0

Please sign in to leave a comment.