gpt4 book ai didi

MySQL - IF something, THEN also select 哪里

转载 作者:行者123 更新时间:2023-11-29 01:44:02 25 4
gpt4 key购买 nike

所以,我有一个令人困惑的 MySQL 问题。我觉得我需要使用一些 IF 语句,但我真的不确定如何在这种情况下实现它们!首先,考虑以下查询。很简单:

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

但是,我需要扩展它,这就是我遇到的问题。想来想去,我不太确定如何解释它,所以下面是表结构,然后是一些例子。

+---------+--------+--------+
| node_id | parent | source |
+---------+--------+--------+
| 1 | 0 | 0 |
+---------+--------+--------+
| 2 | 0 | 0 |
+---------+--------+--------+
| 3 | 1 | 1 |
+---------+--------+--------+
| 4 | 3 | 0 |
+---------+--------+--------+

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

+-------------+--------+------+-------+
| variable_id | source | name | value |
+-------------+--------+------+-------+
| 1 | 0 | yes | sure |
+-------------+--------+------+-------+
| 2 | 0 | yes | yeah |
+-------------+--------+------+-------+
| 3 | 0 | no | nope |
+-------------+--------+------+-------+
| 4 | 1 | herp | derp |
+-------------+--------+------+-------+

现在,这是我需要根据 :input 发生的事情。


“fish”、“wish”、“sure”或“yeah” ---SELECT flow.node_id 1

  • 这是因为“fish”、“wish”和“*yes*”都与 flow.node_id 1 相关联。请注意,*yes* 被星号包围,而不是“yes"按字面解释,而是从 placeholder_variables 中提取值。

“不”或“不” ---SELECT flow.node_id 2

  • 这是因为“*no*”和“nay”与 flow.node_id 相关联 2. 同样,由于星号,“no”不是字面解释,而是“nope”匹配因为“no”在 placeholder_variables 表中,即使“nope”不在 flow_strings 表中。

“不”和“*不*” ---不匹配

即使 *no* 在 flow_strings 中,它也不应该匹配,因为它周围有星号(和相应的 placeholder_variable),这意味着它不应该按字面解释,因此只能被评估通过其相应的占位符变量的值。


“宝贝” ---不匹配

  • 尽管“baby”周围没有星号,但它对应于 flow.node_id 3,并且该节点的 flow.source 为 1。

"derp" ---不匹配

  • 这是因为 placeholder_variables.source 对于 *herp* 是 1,即使它在 flow_strings 表中也是如此。

"*herp*" ---SELECT flow.node_id 4

  • 尽管 flow_strings 表中 *herp* 周围有星号,但对应的 placeholder_variable.source 为 1。

** 总结**

  1. 没有来源 = 1
  2. 如果 sql_regex 被星号包围,则解释 placeholder_variables,但前提是对应的 placeholder_variable 的 source 为 0。
  3. 如果 source 为 0,并且没有星号,则按字面解释 sql_regex

我知道我可以使用 MySQL 的 SUBSTRING() 来处理星号。我也知道,(因为我使用的是 PHP)理论上我可以将其分成两个查询,然后通过循环第一个查询动态生成第二个查询。但是,这既 A) 内存密集型,又 B) 草率。

所以我的问题是:是否可以单独使用 MySQL 来完成此操作?如果是,您建议我如何格式化它?您不需要为我编写查询,但如果您能帮助我解决一些逻辑问题,我将不胜感激!除了我已经概述的内容之外,我完全不知道该尝试什么,而且我绝对不想那样做。

谢谢!

最佳答案

您可以使用此解决方案:

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.

SQLFiddle Demo

关于MySQL - IF something, THEN also select 哪里,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11811688/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com