I have a car chart that has a status field. I want that at the time of doing the SQL query if its status is ==1
I get the text "on hold". If the status is a 2
that spells "en route", and so on (they are numbers from 1 to 4).
I have a car chart that has a status field. I want that at the time of doing the SQL query if its status is ==1
I get the text "on hold". If the status is a 2
that spells "en route", and so on (they are numbers from 1 to 4).
Good morning partner, you can take three ways to do what you want:
Perform a CASE
; by means of a 'CASE' you can tell the field that it depends on the value that it shows one or the other. Example:
SELECT CASE campo_tabla WHEN 1 THEN 'En Espera' WHEN 2 THEN 'En Ruta' WHEN 3 THEN 'Recibido' WHEN 4 THEN 'Finalizado' ELSE 'Otro Caso' END from nombre_tabla;
Use a IF
; with this you also decide that if a certain value comes in your field show the one you want by the condition, otherwise it shows the value that the field brings. Example:
SELECT IF(campo_tabla = 1, 'En espera', campo_tabla) as "En espera", IF(campo_tabla = 2, 'En ruta', campo_tabla) as "En ruta", IF(campo_tabla = 3, 'Recibido', campo_tabla) as Recibido, IF(campo_tabla = 4, 'Finalizado', campo_tabla) as Finalizado from nombre_tabla;
Create a table that stores the status, that is, a table called status and that has three fields, one that is the id, another the type of status and the third the description of the status. So you join the status table with your car table and send to call the fields you want from each of them and instead of putting the status field of the car table, you put the field that has the type of status. Example:
tabla 'autos' ('id','placa', 'modelo', 'estatus','duenio') tabla 'estatus' ('id','tipo_estatus','descripcion_estatus')
When you do the SELECT
, you do something like this:
SELECT a.placa, a.modelo, e.tipo_estatus, a.duenio FROM autos a, estatus e WHERE a.estatus = e.tipo_estatus;
The letters that precede each field are used to establish which table they belong to FROM
, I hope you serve as indicated.
Greetings.