Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Conditional column for query based on other columns in MySQL

I'm pretty sure I've seen this somewhere, but I can't find the right terminology so I'm having trouble...

Let's say I have a table with user info (let's also assume that it was created by someone who gets paid more than me, so modifying the schema is not an option.) Among the various columns of user info are columns for DOB and job title. I want a query that, based on what is in those columns, will include an extra column called "Real_Title", for example:

User_id    Job_Title    DOB
  joe_1      manager    01/01/1950
  jim_1    associate    01/01/1970
 jill_1    associate    01/01/1985
 jane_1      manager    01/01/1975

query:

SELECT User_id, Real_Title FROM users
IF (YEAR(DOB) < 1980 AND Job_Title = "manager")
   {Real_Title = "Old Fart"}
ELSE IF (YEAR(DOB) < 1980 AND Job_Title = "associate")
   {Real_Title = "Old Timer"}
ELSE IF (YEAR(DOB) > 1980 AND Job_Title = "manager")
   {Real_Title = "Eager Beaver"}
ELSE IF (YEAR(DOB) > 1980 AND Job_Title = "associate")
   {Real_Title = "Slacker"}

I know the above is not only wrong but also coded really inefficient, but I wanted to get the idea across.

Is there a way, without using joins, to populate a column based on information in one or more other columns in the same table?

Currently I'm using something in the PHP script after the results are obtained to channel those results into the groups I want, but if it can be done in the query, that would make porting the query to other scripts and languages much easier.

Thanks!

like image 419
Anthony Avatar asked May 11 '09 18:05

Anthony


People also ask

How do I match two columns in MySQL?

Here's the generic SQL query to two compare columns (column1, column2) in a table (table1). mysql> select * from table1 where column1 not in (select column2 from table1); In the above query, update table1, column1 and column2 as per your requirement.

Can we use if statement in select query in MySQL?

Answer: MySQL IF() function can be used within a query, while the IF-ELSE conditional statement construct is supported to be used through FUNCTIONS or STORED PROCEDURES.

Is between clause in MySQL?

The MySQL BETWEEN OperatorThe BETWEEN operator selects values within a given range. The values can be numbers, text, or dates. The BETWEEN operator is inclusive: begin and end values are included.

Can union work with different columns?

We can apply UNION on multiple columns and can also order the results using the ORDER BY operator in the end.


2 Answers

select User_id
,case 
    when (YEAR(DOB) < 1980 AND Job_Title = "manager")   then 'Old Fart'
    when (YEAR(DOB) < 1980 AND Job_Title = "associate") then 'Old Timer'
    when (YEAR(DOB) > 1980 AND Job_Title = "manager")   then 'Eager Beaver'
    when (YEAR(DOB) > 1980 AND Job_Title = "associate") then 'Slacker'
    else 'nobody'
end
as Real_Title 
from users
like image 97
feihtthief Avatar answered Sep 19 '22 05:09

feihtthief


If I understand correctly, I think you're looking for the CASE statement:

SELECT User_id,
        (CASE
            WHEN YEAR(DOB) < 1980 AND Job_Title = "manager" THEN "Old Fart"
            WHEN YEAR(DOB) < 1980 AND Job_Title = "associate" THEN "Old Timer"
            ...
            ELSE "Unknown Title"
        END) AS Real_Title
FROM users;
like image 31
Chad Birch Avatar answered Sep 21 '22 05:09

Chad Birch