PHP | MYSQL Error: Operand should contain 10 column(s)

Asked

Viewed 56 times

0

I am trying to develop a Query where, need to be Consulted in 6 equal tables if there is any specific value.

Here is my Query:

SELECT (
    CASE WHEN (SELECT * FROM sala1 WHERE aluno = '$AlunoInformado') = 1 THEN END 
    ELSE
        CASE WHEN(SELECT * FROM sala2 WHERE aluno = '$AlunoInformado') = 1 THEN END
        ELSE
            CASE WHEN(SELECT * FROM sala3 WHERE aluno = '$AlunoInformado') = 1 THEN END
            ELSE
                CASE WHEN(SELECT * FROM sala4 WHERE aluno = '$AlunoInformado') = 1 THEN END
                ELSE
                    CASE WHEN(SELECT * FROM sala5 WHERE aluno = '$AlunoInformado') = 1 THEN END
                    ELSE
                        CASE WHEN(SELECT * FROM sala6 WHERE aluno = '$AlunoInformado') = 1 THEN END
                        END
                    END
                END
            END
        END
    END
)entrada FROM $SelectSalas WHERE status = 0

OBS: $AlunoInformado receives a value you can have in all tables.

But when you query, it presents Operand should contain 10 column(s).

What’s wrong with the query? Why not present the informed column entrada as instructed before the FROM ?

  • Isn’t a simple union better? http://answall.com/questions/163399/como-buscar-um-mesmo-termo-em-4-tabelas-different - and it would not be the case that the tables are organized so as to have a relation pupil-room instead of several tables of room?

  • @Bacco Not for this case, because I need to separate by rooms and by day of scheduling. As I am not very good at SQL, this was the simplest and fastest way it worked. UNION is not returning results, I do not know the reason, but does not return.

  • If you post a small sample data collection to http://SQLFiddle.com, I can try to help. Without the data and table structure, it gets more complicated. As to separate the rooms, if you have a "room" column with the number, just use a WHERE to choose the desired room.

  • Can you post the table structure you are using? Only the Structure, does not need to specify the data, but the times that this error returned to me, was because the table I was using in FROM had less or more columns than the amount returned in the function SELECT

1 answer

0


CASE WHEN (SELECT * FROM sala1 WHERE aluno = '$AlunoInformado') = 1 THEN END 
    ELSE

I believe your CASE WHEN is wrong, because you are putting END (the command that terminates CASE) before your ELSE.

And as far as I know, you need to have a result for your "WHEN" ie, when what is after the WHEN is true, do x thing, if not (that would be the ELSE) do y thing.

Thinking the way I said it, the code would be like:

SELECT (
   CASE 
      WHEN (SELECT * FROM sala1 WHERE aluno = '$AlunoInformado') = 1 THEN 'sala1'
      ELSE
         CASE 
            WHEN(SELECT * FROM sala2 WHERE aluno = '$AlunoInformado') = 1 THEN 'Sala2'
            ELSE 'Não foi encontrado'
         END
   END
) AS entrada 
FROM $SelectSalas 
WHERE status = 0

And I would indicate you put something more "concrete" in your WHEN, because there you are selecting all columns of numerous tables, select something x from each table. For example the id of the same, so it is easier to validate your condition. Type:

SELECT
   CASE
      WHEN (SELECT tabela_id FROM tabela_x WHERE cod_aluno = '$código') > 1 THEN 'Exite'
      ELSE 'Não existe'
   END

I hope I helped :D

https://msdn.microsoft.com/pt-br/library/ms181765.aspx

  • It didn’t solve the problem, but it gave me a direction to solve, thank you very much @Jonathan-machado

Browser other questions tagged

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