Function in MySQL to return text according to numeric index

4

I have a table named tb_usuarios with columns

id_usuario
nome
email
telefone
tipo_de_acesso

I would like to create a function in MySQL, where when using echo $row['tipo_de_acesso'] , the correct type already exists, but in text mode, not numeric, among the ones that I delimit:

Tipo = 1: Administrador
Tipo = 2: Usuário
Tipo = 3: Cliente

How can I create a function inside MySQL to automate this, instead of returning 1 , 2 or 3 , then return Administrador , Usuário or Cliente ?     

asked by anonymous 10.04.2017 / 21:11

1 answer

7

Two solutions with MySQL:


Function ELT()

SELECT ELT( tipo, 'administrador', 'usuario', 'cliente');

ELT returns the string of the position indicated by the first parameter, in this case the column corresponding to tipo .

The advantages of ELT are that you do not need to tweak the table structure, and you can decide its use when writing SELECT .

The disadvantage is that if you use the column in SELECT * (which is no longer the most recommended in all situations) it will return numerically, forcing it literally to use all SELECT that needs textual information. >

  

Manual: link


Column ENUM

Maybe it's simpler to convert the column to

 ENUM ( 'Administrador', 'Usuário', 'Cliente' )

See ENUM running on SQL Fiddle .

The advantages are that it returns in text in both% w and% w%, and still allow numeric return with w w w w w w w w w w w w w w w w w w w w w w w w w w Its internal storage is numeric, spending the same as the smaller integer needed for the number of options. Also, it works with SELECT * and also with SELECT tipo , and likewise you can make SELECT 0+tipo with text or integer, both working normally.

In addition, you can convert a numeric column to WHERE tipo = 2 without losing any existing numeric data, it is only important to take care of converting with the items in the correct order.

The main disadvantage is that when adding, removing or editing any type, you need to tweak the table structure, and it requires extra care when reordering the types so as not to change the logical order in order to "shuffle" the data already inserted. Also, depending on how much you want to respect the layers of the application, "embedding" this type of information in the table structure may hurt some principles, but there is a question of understanding the pros and cons in your specific case. >

  

Manual: link

If it were in PHP

This is completely different from what you requested, but I will only leave an example as an alternative for eventual visitors. A simple PHP equivalent (which I took as the basis of your example in the question) would be this:

Before a display loop, something like this:

$tipos_de_acesso=array('', 'administrador', 'usuario', 'cliente');

( WHERE tipo = 'Usuário' column is for zero index because INSERT is one)

And in loop , instead of ENUM , use this:

echo $tipos_de_acesso[$row['tipo_de_acesso']];

Obviously you should take care to avoid indices larger than the array size, but it was just to illustrate one of the simpler paths.

    
10.04.2017 / 21:22