Query return interaction with Array

Asked

Viewed 55 times

2

SQL:

SELECT t.idMembro, t.nomeTabela from (

          SELECT idMembro as idMembro, 'pastores' as  nomeTabela FROM pastores
    union SELECT idLider as idMembro, 'redes'   as  nomeTabela FROM redes
    union SELECT idLider as idMembro, 'regioes' as  nomeTabela FROM regioes
    union SELECT idLider as idMembro, 'areas'   as  nomeTabela FROM areas
    union SELECT idLider as idMembro, 'setores' as  nomeTabela FROM setores
    union SELECT idLider as idMembro, 'celulas' as  nomeTabela FROM celulas
) t
WHERE t.idMembro = " . $_idMembro;

That gives me the following return:

inserir a descrição da imagem aqui

Now I have the following array of priorities:

$prioridades = array (

    'pastores' => 5,
    'redes'   => 4,
    'regioes' => 3,
    'areas'   => 2,
    'setores'  => 1

);

I need help walking the exit of SQL and check together with the array priorities above which of the outputs has higher value and return in the new array only the highest priority line.

Type:

idMembro: 1
nomeTabela: pastores

Who can help me?

  • Why, instead of setting the table name, you do not set the priority and already sort downwards and return only the first value? So would return [1, 5], but you know that 5 is "shepherds".

  • You can use ORDER BY FIELD(t.nomeTabela, 'pastores', 'redes', 'regioes', 'areas', 'setores') along with LIMIT 1. Is the last example here.

  • Fernando Savio. So wrong? ORDER BY FIELD('pastors', 'networks', 'regions', 'areas', 'sectors') LIMIT 1, that is, removing t.nomeTable, from the clause ORDER BY. Because it was giving cells as higher value and not shepherds

No answers

Browser other questions tagged

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