Query in a column with different values

Asked

Viewed 573 times

2

In my project, I have a SELECT which brings me back two names, but these names are different. On my command, I need to return these two different names that are in the same column of the bank in a tag of a xml that I’m creating.

How could I make a method that would recognize these two names in and return them to me?

The way I got it here, it returns only one.

I’m gonna put my SELECT to help!

SELECT *
FROM TabelaPessoa 
INNER JOIN TabelaRegistro ON PessoaCodCasamentoCodigo = CasamentoCodigo 
INNER JOIN LocalCasamento ON LocalCasamentoCodigo = CasamentoCodigo 
LEFT JOIN CasamentoRegime ON RegimeCodigo = CasamentoCodigo 
LEFT JOIN Infomacao ON InformacaoCodigo = CasamentoCOdigo
WHERE NomePessoa = 'Fulano' OR
      NomePessoa = 'Fulana' 
AND CodigoTipoPessoa IN (1, 7)

EDIT

Codes C# that I have:

        try
        {
            string sqlPesquisa = "";

            sqlPesquisa = "SELECT * \n" +
            "FROM TabelaPessoa  INNER JOIN TabelaRegistro ON \n" +
            "     PessoaCodCasamentoCodigo = CasamentoCodigo INNER JOIN LocalCasamento ON \n" +
            "     LocalCasamentoCodigo = CasamentoCodigo LEFT JOIN CasamentoRegime ON \n" +
            "     RegimeCodigo = CasamentoCodigo LEFT JOIN Infomacao ON \n" +
            "     InformacaoCodigo = CasamentoCodigo \n" +
            "WHERE NomePessoa = " + tabela + " OR \n" +
            "      NomePessoa = " + tabela + " AND \n" +
            "      CodigoTipoPessoa IN (1, 7)";

            DataTable pesquisa = executarPesquisa(sqlPesquisa);

            return (pesquisa);
        }
  • Put the code in C# of your method too so someone can help you.

  • I don’t see any problem with the code. I think it is the case of making a SQL Fiddle to validate the data.

  • Why do you do + tabela +? You’re passing the same name and comparing it twice in the same way... there’s something wrong

  • @Gypsy Rhythmandmendez what happens is that at the time of returning me the data, I say the names, only returns me one of the names, and repeats it

  • + tabela + is the table value I want the data to come!

  • So. Put the data in SQL Fiddle and let’s see what happens.

  • The tricky thing is that there are several tables I have here, and I do the joins in them. There are 4 tables.

  • 2

    His code has "+ table +" twice, he is looking for the same name twice. Also, as Fernando answered, parentheses are missing... AND has preference over OR. Thus, OR B AND C is the same thing as A OR (B AND C).

Show 3 more comments

2 answers

3


Your command never will return two different names because you are searching only ONE name, and the name you are searching comes from a variable called tabela. Your command should be:

 where 
     ( NomePessoa = '" + Nome_1 + "' OR NomePessoa = '" + Nome_2 + "' )
 AND ( CodigoTipoPessoa in (1,7) ) 

where

 Nome_1 = 'Fulano'
 Nome_2 = 'Fulana'

And you’ll only list John Doe or Jane Doe CodigoTipoPessoa is between 1 and 7

1

I would recommend separating the name into two columns in your database. An alternative, considering two names:

string doisNomes = "Fulano Sicrano";
string[] arrayDeDoisNomes = doisNomes.Split(' ');
string nomeUm = arrayDeDoisNomes[0]; // aqui está "Fulano"
string nomeDois = arrayDeDoisNomes[1]; // aqui está "Sicrano"

The Split method takes a char (or a string) as delimiter, a character that identifies the "cut". In the above example I used a blank space.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.