Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7680531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T18:08:06+00:00 2026-05-31T18:08:06+00:00

I have a table conditions, which defines some conditions. I have another table called

  • 0

I have a table conditions, which defines some conditions.
I have another table called rules which defines some rules, and each rule can have many conditions. However, the rule must be able to have grouped conditions in this manner:

(condition1) && 
(condition2) && 
    (condition3 
    || condition4 
    || 
       (condition6 && 
       condition7)
    ) && 
(condition5)

Since this nesting can go as deep as it wants, I can’t bind these two tables with a bind table, so I was thinking of actually saving it into the rules table as such:
(condition1)&&(condition2)&&(condition3||condition4||(condition6&&condition7))&&(condition5)
where “condition1” would be the ID of condition 1, etc.

I would then parse this information in PHP, and somehow create a logical check from the entire string.

Is this an OK approach, or is there a better way to do this kind of nested binding withing MySQL? I absolutely need to keep the ability to group conditions in both AND and OR subgroups on a single rule.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T18:08:07+00:00Added an answer on May 31, 2026 at 6:08 pm

    I would certainly steer clear of storing your conditions as a string. You’ll have to parse that string to use it and if you want to edit an pre-existing conditions (or search for certain conditions etc) you will probably end up tying yourself in knots.

    I would certainly store it in some form of tree structure although @tpaksu’s solution has a few flaws. I would suggest something along these lines

    ID  |  Rule_ID   |  Condition_ID    |  Parent   |  Operator |
    ------------------------------------------------------------
    1   |  1         |  NULL            | NULL      |  AND      |
    2   |  1         |  1               | 1         |  NULL     |
    3   |  1         |  2               | 1         |  NULL     |
    4   |  1         |  NULL            | 1         |  OR       |
    5   |  1         |  3               | 4         |  NULL     |
    6   |  1         |  4               | 4         |  NULL     |
    7   |  1         |  NULL            | 4         |  AND      |
    8   |  1         |  6               | 7         |  NULL     |
    9   |  1         |  7               | 7         |  NULL     |
    10  |  1         |  5               | 4         |  NULL     |
    

    I think this represents the original logical structure you wanted to model. The advantage of this is you can have several branches in your tree where there are conditions at the same level and you can trace which branch each condition belongs to – you don’t have to rely on the order of the entries in the table. The parent column refers to the table’s main ID column.

    You basically have two types of row (which suggests to me you may be able to split this into two tables):

    • a row with an operator is a “node” that combines one or more conditions/nodes using one of the two logical operators (but I guess you could use any logical operator). These nodes have no condition_id
    • a row with a condition_id represents a single condition (I’ve numbered them as per your example). These rows have no operators as you can’t have an operator on a single operand. I’m not taking into account whether your base conditions do themselves use the same logical operators as you’ve abstracted that detail in your question.

    I’m not sure you need the rule_id as well. In this example, you don’t but if you wanted to store meta-info about each rule (e.g. name/label, valid date range etc) you might want to extract that out into a separate table. It might also make it easier to reconstruct the logic in PHP as MySQL doesn’t easily handle recursive queries AFAIAA.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have a table of data, which can have each other as parents or
I have table called page which represents every single page in my website. page_id
I have a table which has some nullable fields and when the user enters
I have a table called user_relationship. which has two foreign keys refering back to
Right now I have a table called Campaigns that has many Hits, if I
I have a database with a table which is full of conditions and error
I have the following Execution statement which creates a table (using data from another
I have a table in DB called TABLE_3 which has several columns....and I wanna
I have a relationship table in a rails application called edit_privileges , in which
I have a table which maintains categories from another site. I populate this table

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.