我需要比较2个表,其中一个具有数据,另一个具有解释。
因此,数据表如下所示:
id | state | substate | stage | suppressed
001 wip A1 B3 Y
003 wip A1 B1 N
005 done A2 B3 Y
009 wip A1 B3 N
... and many more similar
说明表必须获取上面的4种状态(状态,子状态,阶段,已抑制)并将其转换为人类可读的形式:
state | substate | stage | suppressed | HRoutput1 | HRoutput2
wip A1 B1 N "it's ok" "wait...dont do anything"
wip A1 B3 N "it's not ok" "better call saul"
done A2 B3 Y "it's not ok" "forget about it"
wip A1 B1 Y "it's ok" "something minor needs to be done"
* * * Y "it's ok" "it's suppressed"
done * * * "it's ok" "it's being worked on"
现在看到上面的解释表有两个阶段,其中我在表中使用了通配符
*,*,*,Y,"it's ok","its suppressed"
当状态,子状态,阶段和抑制在上述任何标准中均不匹配时,应使用此选项。
到目前为止,我所做的是将数据上的每一行加载到数组A中
然后让元素数组A遍历说明表:
"SELECT * from explanation_table where state = '" . $data['state'] ."' and '" . $data['substate'] . "' ....etc etc
然后运行查询并将结果保存到说明数组中并打印结果
$data['id'] . $expl['HRoutput1'] . $expl['HRoutput2']
除了带通配符的情况外,我的代码工作正常。
最佳答案
要从查询中恢复,您可以执行以下操作:
"SELECT * from explanation_table where (state = '" . $data['state'] ."' OR state = '*') and (substate = '" . $data['substate'] . "' OR substate = '*') ....etc etc.... ORDER BY state != '*', substate != '*' ... LIMIT 1
这样,您将同时匹配通配符和精确的state / substate / ...匹配,将实际匹配的值设置为比通配符匹配(顺序为)更高,并且仅返回最佳匹配结果。
关于php - 比较2个表和通配符,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/8216969/