[英]MySQL - IF something, THEN also select where
So, I have a confusing MySQL issue. 因此,我有一个令人困惑的MySQL问题。 I feel like I need to use some IF statements, but I'm really not sure how to implement them into this situation!
我觉得我需要使用一些IF语句,但是我真的不确定如何在这种情况下实现它们! 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.
仔细考虑一下,我不太确定如何解释它,因此下面是表结构和一些示例。
flow
flow
+---------+--------+--------+
| node_id | parent | source |
+---------+--------+--------+
| 1 | 0 | 0 |
+---------+--------+--------+
| 2 | 0 | 0 |
+---------+--------+--------+
| 3 | 1 | 1 |
+---------+--------+--------+
| 4 | 3 | 0 |
+---------+--------+--------+
flow_strings
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* |
+----------------+---------+-----------+
[ ... ]
placeholder_variables
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
. 现在,这是我需要基于
:input
发生的事情。
"fish", "wish", "sure", or "yeah" --- SELECT flow.node_id
1 “鱼”,“希望”,“确定”或“是” --- 选择
flow.node_id
1
flow.node_id
1. Note that *yes* is surrounded by asterisks, so instead of "yes" being interpreted literally, it instead draws the values from placeholder_variables
. flow.node_id
1相关联。请注意,“ * yes *”用星号包围,因此它不是按字面解释而是用“来自placeholder_variables
值。 "nope" or "nay" --- SELECT flow.node_id
2 “ nope”或“ nay” --- 选择
flow.node_id
2
flow.node_id
2. Again, because of the asterisks, "no" is not interpreted literally, but "nope" matches because "no" is in the placeholder_variables
table, even though "nope" is not in the flow_strings
table. flow.node_id
2相关联。同样,由于星号,“ no”不能按字面意义解释,但是“ nope”可以匹配,因为placeholder_variables
表中为“ no”,即使flow_strings
表中没有“ flow_strings
。 "no" and "*no*" --- NO MATCH “ no”和“ * no *” - 没有匹配
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). 即使* no *在
flow_strings
,它也不应该匹配,因为它周围带有星号(和一个相应的placeholder_variable),这意味着它不应按字面意义进行解释,因此只能通过其相应的占位符变量的值进行评估。
"baby" --- NO MATCH “宝贝” --- 不匹配
flow.node_id
3, and that node's flow.source
is 1. flow.node_id
3,该节点的flow.source
为1。 "derp" --- NO MATCH “ derp” --- 不匹配
placeholder_variables.source
is 1 for *herp*, even though it is in the flow_strings
table. flow_strings
表中,* herp *的placeholder_variables.source
为1。 "*herp*" --- SELECT flow.node_id
4 “ * herp *” --- SELECT
flow.node_id
4
flow_strings
table, the corresponding placeholder_variable.source
is 1. flow_strings
表中* herp *周围有星号,对应的placeholder_variable.source
也为1。 ** TO SUM UP ** ** 总结一下 **
source
= 1 source
= 1 sql_regex
is surrounded by asterisks, but only if the corresponding placeholder_variable's source
is 0. sql_regex
用星号包围,则解释placeholder_variables,但仅当相应的placeholder_variable的source
为0时解释。 source
is 0, and no asterisks are present, interpret sql_regex
literally. source
为0,并且没有星号, sql_regex
字面意义解释sql_regex
。 I know that I can use MySQL's SUBSTRING()
to work with the asterisks. 我知道我可以使用MySQL的
SUBSTRING()
处理星号。 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. 我也知道,(当我使用PHP时)从理论上讲,我可以将其分为两个查询,然后通过遍历第一个查询来动态生成第二个查询。 However, this would be both A) memory intensive, and B) sloppy.
但是,这既是A)占用大量内存,又是B)草率。
So my question is this: Is it possible to do this using MySQL alone? 所以我的问题是:是否可以仅使用MySQL来做到这一点? 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: 您可以使用以下解决方案:
SELECT a.*,
COALESCE(c.value, b.sql_regex) AS string #-- If there was a successful JOIN (sql_regex was a variable), then display the value of the "value" column in placeholder_variables, otherwise if the JOIN did not succeed (sql_regex was a literal value), just display sql_regex instead.
FROM flow a
JOIN flow_strings b ON a.node_id = b.node_id
LEFT JOIN placeholder_variables c #-- LEFT JOIN placeholder_variables on these conditions:
ON b.sql_regex LIKE '*%*' AND -- That sql_regex is a variable
REPLACE(b.sql_regex, '*', '') = c.name AND -- Match sql_regex up with the "name" column in placeholder_variables. We must replace the asterisks in sql_regex so that the values can match ("name" column do not contain asterisks)
c.source = 0
WHERE a.source = 0 AND
COALESCE(c.value, b.sql_regex) = :input -- If the string was interpreted as a placeholder variable, make the condition on the interpreted value in the placeholder_variables table ("name" column), otherwise, just make the condition on the sql_regex column.
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.