简体   繁体   English

mySQL 查询 — 多个可能的特定值

[英]mySQL query — multiple possible specific values

I am currently querying our database using a rather lengthy statement, given how little it does.我目前正在使用相当长的语句查询我们的数据库,因为它的作用很小。

fieldname like 'expected_result_up' AND `result_text` not like 'obsolete' and
AK47_testsection_id like 436 and ( `resultsfields_value` like 240 or
`resultsfields_value` like 846 or `resultsfields_value` like 1000 )

I'm trying to get a value from "resultsfields_value" such that it can be either 240, 846, or 1000. The current query works, it returns every one of the values.我试图从“resultsfields_value”中获取一个值,它可以是 240、846 或 1000。当前查询有效,它返回每个值。 But it only works in a manual query through the heidiSQL interface.但它只适用于通过 heidiSQL 接口进行的手动查询。

When I run the same query with different syntax through php I only get the last "or" (1000) returned.当我通过 php 以不同的语法运行相同的查询时,我只得到最后一个“或”(1000)返回。 The php DB query is: php DB 查询是:

"SELECT point_id FROM v_ak47_test_point WHERE ak47_testsection_id= $secID
and result_text != 'obsolete' and fieldname like 'expected_result_up' and 
(resultsfields_value = 240 or resultsfields_value = 846 or resultsfields_value =
1000)"

Is there a more elegant way to specify multiple unique possibilities?有没有更优雅的方式来指定多种独特的可能性?

You can use an IN clause:您可以使用 IN 子句:

SELECT point_id 
FROM v_ak47_test_point 
WHERE ak47_testsection_id= $secID
AND result_text != 'obsolete'
AND fieldname = 'expected_result_up'
AND resultsfields_value IN (240, 846, 1000)

Use the IN () clause.使用IN ()子句。 Because you haven't put parens around the last three OR conditions, the last of them, if true, will override everything else in the WHERE clause.因为您没有在最后三个OR条件周围放置括号,所以最后一个如果为真,将覆盖WHERE子句中的所有其他内容。

SELECT point_id FROM v_ak47_test_point WHERE ak47_testsection_id= $secID
  and result_text != 'obsolete' and fieldname like 'expected_result_up' and 
  resultsfields_value = IN (140, 846, 1000)

Also, since you have no % wildcard with fieldname like 'expected_result_up' , don't use LIKE where a simple fieldname = 'expected_result_up' will do.此外,由于您没有%通配符和fieldname like 'expected_result_up' ,所以不要在简单的fieldname = 'expected_result_up'地方使用LIKE (Unless that's just placeholder data, and you actually intend to use something more complicated) (除非这只是占位符数据,并且您实际上打算使用更复杂的东西)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM