在PHP中选择WITH LIKE和QUERE QUERY

时间:2016-07-17 12:23:41

标签: php mysql sql

我遇到了用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.

请帮帮我。感谢

1 个答案:

答案 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 /编程,那么只要混合使用ANDOR运算符,就可以使用括号。这将在未来防止此类问题。