我遇到了用PHP中的条件选择查询的问题
SELECT AES_DECRYPT(v_name, 'k7ewjks93ls82!!') as v_name,
AES_DECRYPT(v_email, 'k7ewjks93ls82!!') as v_email,
AES_DECRYPT(v_id_no, 'k7ewjks93ls82!!') as v_id_no,
AES_DECRYPT(v_phone, 'k7ewjks93ls82!!') as v_phone,
v_id, c_id, v_purpose
FROM visitor
WHERE c_id = '1' AND
AES_DECRYPT(v_name, 'k7ewjks93ls82!!') LIKE '%a%' OR
AES_DECRYPT(v_email, 'k7ewjks93ls82!!') LIKE '%a%' OR
AES_DECRYPT(v_id_no, 'k7ewjks93ls82!!') LIKE '%a%' OR
AES_DECRYPT(v_phone, 'k7ewjks93ls82!!') LIKE '%a%' OR
v_purpose LIKE '%a%'
I am trying to query search with 'a' but using c_id = 1, but these are the actual results im getting
I need to be querying viewingthese results instead.
请帮帮我。感谢
答案 0 :(得分:0)
你只需要在正确的地方使用括号。我想你打算:
SELECT AES_DECRYPT(v_name, 'k7ewjks93ls82!!') as v_name, AES_DECRYPT(v_email, 'k7ewjks93ls82!!') as v_email,
AES_DECRYPT(v_id_no, 'k7ewjks93ls82!!') as v_id_no, AES_DECRYPT(v_phone, 'k7ewjks93ls82!!') as v_phone,
v_id, c_id, v_purpose
FROM visitor
WHERE c_id = '1' AND
(AES_DECRYPT(v_name, 'k7ewjks93ls82!!') LIKE '%a%' OR
AES_DECRYPT(v_email, 'k7ewjks93ls82!!') LIKE '%a%' OR
AES_DECRYPT(v_id_no, 'k7ewjks93ls82!!') LIKE '%a%' OR
AES_DECRYPT(v_phone, 'k7ewjks93ls82!!') LIKE '%a%' OR
v_purpose LIKE '%a%'
);
(注意:我不确定v_purpose
是否应该在OR
逻辑中。)
如果您正在学习SQL /编程,那么只要混合使用AND
和OR
运算符,就可以使用括号。这将在未来防止此类问题。