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 the case of the column corresponding to the tipo
.
The advantages of ELT
are the fact that it does not need to touch the table structure, and can decide its use when designing the SELECT
.
The downside is that if you use the column in one SELECT *
(which is no longer the most recommended in all situations) will return numerically, forcing its literal use throughout SELECT
who needs the textual information.
Handbook: https://dev.mysql.com/doc/refman/5.5/en/string-functions.html#function_elt
Column ENUM
Perhaps simpler is to convert the column to
ENUM ( 'Administrador', 'Usuário', 'Cliente' )
See the ENUM working on SQL Fiddle.
The advantages are that it returns in text both in a SELECT *
how much SELECT tipo
, and still allow the numerical return with SELECT 0+tipo
. Its internal storage is numerical, spending the same as the smallest integer needed for the number of options. Also, it works with WHERE tipo = 2
and also with WHERE tipo = 'Usuário'
, and in the same way you can do the INSERT
text or full text, both working normally.
Also, you can convert a numerical column to ENUM
without losing existing numerical data, it is only important to take care to make the conversion with the items in the correct order.
The main drawback is that when adding, removing or editing any type, you need to tinker with 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 entered. Also, depending on how much you want to respect the application layers, "embedding" this type of information into the table structure may hurt some principles, but then it’s a matter of you understanding the pros and cons in your specific case.
Handbook: https://dev.mysql.com/doc/refman/5.7/en/enum.html
If it were in PHP
This is completely different from what you asked, but I’ll just leave an example as an alternative for any visitors. A simple PHP equivalent (based on your example in the question) would be this:
Before an exhibition loop, something like this:
$tipos_de_acesso=array('', 'administrador', 'usuario', 'cliente');
(the column ''
is for the zero index, as the 'administrador'
is a)
And in the loop, instead of echo $row['tipo_de_acesso'];
, use this:
echo $tipos_de_acesso[$row['tipo_de_acesso']];
Obviously you should take some care to avoid higher indices than the size of the array, but it was only to illustrate one of the simplest ways.
Can you explain or edit the question? The excerpt
echo $var...
does not apply to Mysql.– Maurivan
I will not have editable, there will be only 3 levels (1, 2 and 3)... I use Enum to tell me between 1 and 3, but I would like to get in mysql instead of returning 1, return Administrator
– Sr. André Baill
@Edilson the idea is to create a database even, within mysql... to do this, very simple
– Sr. André Baill
From what I understand you’re wanting the type of access in php, right?
– Maurivan
Exactly, but I would like to return from mysql directly, without me having to do if() inside PHP or SELECT, @Maurivan
– Sr. André Baill
Ok. I suggest creating a table for example
Perfil
with the characteristics of each profile and usejoin
to display the user profile. You have some php class for the user?– Maurivan
No, that would also be the correct @Maurivan and use Join, but that’s not what I wanted to do
– Sr. André Baill
No, that would also be correct @Maur
– Sr. André Baill
@Andrébaill I suggest creating a class for
usuario
as it does not wish to use the appeals submitted by colleagues. In this class you should have a method where you would display all user data according to oneid
orusername
passed as parameter.– Maurivan