So, I have a confusing MySQL issue. I feel like I need to use some IF statements, but I’m really not sure how to implement them into this situation! First, consider the following query. It’s simple:
SELECT *
FROM flow
INNER JOIN flow_strings
USING(node_id)
WHERE
(
flow.parent = 0
OR flow.parent = :user_flow
)
AND flow.source = 0
AND :input LIKE flow_strings.sql_regex
However, I need to expand it, and that’s where I’m stuck. Thinking through this, I’m not really sure how to explain it, so following are the table structures, and then some examples.
TABLE flow
+---------+--------+--------+
| node_id | parent | source |
+---------+--------+--------+
| 1 | 0 | 0 |
+---------+--------+--------+
| 2 | 0 | 0 |
+---------+--------+--------+
| 3 | 1 | 1 |
+---------+--------+--------+
| 4 | 3 | 0 |
+---------+--------+--------+
TABLE flow_strings
+----------------+---------+-----------+
| flow_string_id | node_id | sql_regex |
+----------------+---------+-----------+
| 1 | 1 | fish |
+----------------+---------+-----------+
| 2 | 1 | wish |
+----------------+---------+-----------+
| 3 | 1 | *yes* |
+----------------+---------+-----------+
| 4 | 2 | *no* |
+----------------+---------+-----------+
| 5 | 2 | nay |
+----------------+---------+-----------+
| 6 | 3 | *herp* |
+----------------+---------+-----------+
[ ... ]
TABLE placeholder_variables
+-------------+--------+------+-------+
| variable_id | source | name | value |
+-------------+--------+------+-------+
| 1 | 0 | yes | sure |
+-------------+--------+------+-------+
| 2 | 0 | yes | yeah |
+-------------+--------+------+-------+
| 3 | 0 | no | nope |
+-------------+--------+------+-------+
| 4 | 1 | herp | derp |
+-------------+--------+------+-------+
NOW, here’s what I need to happen based on :input.
"fish", "wish", "sure", or "yeah" —SELECT flow.node_id 1
- This is because "fish", "wish", and "*yes*" are all associated with
flow.node_id1. Note that *yes* is surrounded by asterisks, so instead of "yes" being interpreted literally, it instead draws the values fromplaceholder_variables.
"nope" or "nay" —SELECT flow.node_id 2
- This is because "*no*" and "nay" are associated with
flow.node_id2. Again, because of the asterisks, "no" is not interpreted literally, but "nope" matches because "no" is in theplaceholder_variablestable, even though "nope" is not in theflow_stringstable.
"no" and "*no*" —NO MATCH
Even though *no* is in flow_strings, it should not match because it has asterisks around it (and a corresponding placeholder_variable) which means it should not be interpreted literally, and so can only be evaluated by its corresponding placeholder variable’s value(s).
"baby" —NO MATCH
- Even though "baby" does not have asterisks around it, it corresponds to
flow.node_id3, and that node’sflow.sourceis 1.
"derp" —NO MATCH
- This is because
placeholder_variables.sourceis 1 for *herp*, even though it is in theflow_stringstable.
"*herp*" —SELECT flow.node_id 4
- Even though there are asterisks around *herp* in the
flow_stringstable, the correspondingplaceholder_variable.sourceis 1.
** TO SUM UP **
- No
source= 1 - Interpret placeholder_variables if the
sql_regexis surrounded by asterisks, but only if the corresponding placeholder_variable’ssourceis 0. - If
sourceis 0, and no asterisks are present, interpretsql_regexliterally.
I know that I can use MySQL’s SUBSTRING() to work with the asterisks. I also know that, (as I am using PHP) I could theoretically split this into two queries, and then dynamically generate the second query by looping through the first. However, this would be both A) memory intensive, and B) sloppy.
So my question is this: Is it possible to do this using MySQL alone? If yes, how would you recommend I format it? You don’t need to write the query for me, but if you could help me with some of the logic, I’d be very grateful! I have absolutely no idea what to try besides what I have already outlined, and I definitely don’t want to do that.
Thanks!
You can use this solution:
SQLFiddle Demo