简体   繁体   中英

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. But it only works in a manual query through the heidiSQL interface.

When I run the same query with different syntax through php I only get the last "or" (1000) returned. The php DB query is:

"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:

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. 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.

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. (Unless that's just placeholder data, and you actually intend to use something more complicated)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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