How to do a select with several 'where like' field %% in C #

2

The select below works, but it searches all fields and would like idaluno , nomealuno and cpf to be searched and sorted by idaluno

SELECT idaluno,nomealuno,responsavel,cpf,rg,fone_contato,desistente 
    FROM aluno 
    WHERE idaluno LIKE '%12%' OR nomealuno LIKE '%%'  OR cpf LIKE '%%' 
    ORDER BY idaluno;
    
asked by anonymous 26.07.2014 / 21:51

3 answers

2

The SQL that is used in C # has some peculiarities, for you to make a query using LIKE , you should be aware of some things ..

  • Whether the value will not be null or empty "" , Empty

Also be aware of NEVER CONCAT , the query strings. In other words, the response from @Angelo, can suffer with sql injection attacks. Whatever this is talk to another question ...

My answer to your problem ..

using(MySqlCommand cmd = new MySqlCommand(@"SELECT passe_os_campos_relevantes_para_voce_aqui FROM aluno WHERE ((idaluno = @id) OR (nomealuno LIKE '%' @nomealuno '%') OR (cpf = @cpf))"), new MySqlConnection("passe_sua_string_de_conexao")){
    cmd.Parameters.AddWithValue("@id", id);
    cmd.Parameters.AddWithValue("@nomealuno", nome);
    cmd.Parameters.AddWithValue("@cpf", cpf);
}

I recommend never compare with LIKE, primary key values, there is no why, and neither with the CPF field, because the values will NEVER be approximated or are they or are they !! Create a connection class. I do not know if you did, but since your question is simple, it's worth a few more tips! ; D

Thanks!

    
27.07.2014 / 15:49
1

Please try as follows

Select idaluno, nomealuno,responsal,cpf,rg,fone_contato,desistente
from aluno
where idaluno = (like '%"+ @idaluno +"%')
OR
(nomealuno = like'%"+ @nomealuno +"%')
OR
(CPF='"+ @cpf_aluno+"');

remember to put this script in a string that can be executed then passing the data by parameter! (AddParameter, if saved I'm wrong it's something in this type - as soon as I get home, here's an example of my application).

I recommend you to search only by CPF, or just by RG. In this context, the bank may return several results and it will not be easy for the user to find what you really need.

A search by Name or CPF already kills much of this question.

I hope I have helped you!

    
26.07.2014 / 22:04
-2
select * from TABELA1 T1 where CAMPO.T1 in (select T11.CAMPO from TABELA1 T11 
inner join TABELA2 T2 on  T11.CAMPO like '%'||T2.CAMPO||'%')
    
28.09.2018 / 20:59