我有一个问题,我无法找到答案。
如何仅在current_version为1的情况下预先形成搜索查询?我已经尝试了下面的查询但是没有用。
SELECT labtest_id AS id,
labtest_name AS name,
labtest_request_code,
labtest_synonym,
labtest_current_version,
labtest_version
FROM labtests
WHERE labtest_current_version = 1 AND labtest_name LIKE %imap% OR labtest_request_code
LIKE %imap% OR labtest_synonym LIKE %imap%
searchkey = test5
-------------------------------------------------------------------
| id | name | request_code | synonym | current_version | version |
-------------------------------------------------------------------
| 1 | test1 | 74532 | one | 1 | 1.00 |
| 2 | test2 | 74432 | two | 1 | 0.00 |
| 3 | test3 | 78962 | three | 0 | 2.05 |
| 4 | test4 | 72111 | four | 0 | 1.07 |
| 5 | test5 | 90322 | five | 1 | 2.00 |
-------------------------------------------------------------------
因此,在此示例中,搜索查询需要显示第1行
-------------------------------------------------------------------
| id | name | request_code | synonym | current_version | version |
-------------------------------------------------------------------
| 5 | test5 | 90322 | five | 1 | 2.00 |
-------------------------------------------------------------------
答案 0 :(得分:3)
你只是在你的OR
条款和引号周围错过了括号:
SELECT labtest_id AS id,
labtest_name AS name,
labtest_request_code,
labtest_synonym,
labtest_current_version,
labtest_version
FROM labtests
WHERE labtest_current_version = 1
AND (labtest_name LIKE '%imap%'
OR labtest_request_code LIKE '%imap%'
OR labtest_synonym LIKE '%imap%')
如果省略括号,则最后两个OR
子句将返回行labtest_current_version
值,due to operator precedence和布尔定律。
答案 1 :(得分:0)
您需要将OR条件分组为:
WHERE labtest_current_version = 1 AND (labtest_name LIKE %imap% OR labtest_request_code LIKE %imap% OR labtest_synonym LIKE %imap%)
答案 2 :(得分:0)
需要通过将OR部分与()
SELECT labtest_id AS id,
labtest_name AS name,
labtest_request_code,
labtest_synonym,
labtest_current_version,
labtest_version
FROM labtests
WHERE labtest_current_version = 1 AND (labtest_name LIKE %imap% OR labtest_request_code LIKE %imap% OR labtest_synonym LIKE %imap%)