23

I'm trying to Display somes values in my database result, I am using this code but I can not succeed:

SELECT 
   item_code, 
   IF(category_code = 'HERR1', 'NO', 1) OR (category_code = 'COLN5', 'NO', 2) AS category_code, 
   item_name, 
   item_quantity 
FROM qa_items

EDIT : I Want to display for example:

If category_code = 'HERR1'
 Display = 1
else if category_code = 'COLN5'
 Display = 2
End If

If anyone has any idea, would greatly appreciate it

Luuk
  • 12,245
  • 5
  • 22
  • 33
John Nuñez
  • 1,780
  • 13
  • 35
  • 51

7 Answers7

46

I'd rather use CASE :

SELECT item_code, 
CASE category_code 
WHEN 'HERR1' THEN 1
WHEN 'COLN5' THEN 2
ELSE 'NO'
END as category_code, item_name, item_quantity 
FROM qa_items

But IF will also work : IF(category_code='HERR1',1, IF(category_code='COLN5',2,'NO'))

a1ex07
  • 36,826
  • 12
  • 90
  • 103
14

You need to nest the if statements

SELECT item_code, IF(category_code = 'HERR1', 'NO', IF(category_code = 'COLN5', 1, 2)) AS category_code, item_name, item_quantity FROM qa_items

Then the first if will fail and the nested if will evaluate

Mark Willis
  • 1,711
  • 1
  • 14
  • 23
6

Is this what you were after?

SELECT
  item_code,
  CASE category_code
    WHEN 'HERR1' THEN 1
    WHEN 'COLN5' THEN 2
    ELSE 'NO'
  END AS category_code,
  item_name,
  item_quantity
FROM qa_items
user1191247
  • 10,808
  • 2
  • 22
  • 32
1

You can try this. Use IF in select query and update the table you want ;)

create table student(marks int,grade char);

insert into student values(200,null),(120,null), (130,null);

UPDATE student a INNER JOIN (select s.marks, IF(s.marks>=200,'A',IF(s.marks>=130,'B','P')) AS Grade from student s) b on a.marks= b.marks SET a.Grade = b.Grade;

1

Try the following

SELECT item_code, CASE category_code WHEN 'HERR1' THEN 1 WHEN 'COLN5' THEN 0 ELSE 'NONE' END AS category_code, item_name, item_quantity FROM qa_items
Chetter Hummin
  • 6,687
  • 8
  • 32
  • 44
0
SELECT MOBILE,
       CASE (SUBSTRING(mobile, LENGTH(MOBILE), 1)) WHEN ',' 
           THEN SUBSTRING(mobile, 1, LENGTH(MOBILE) - 1)
           ELSE SUBSTRING(mobile, 1, LENGTH(MOBILE)) 
       END AS newmobile
FROM (SELECT CONCAT(IFNULL(`mob1`, ''), IF(`mob1` IS NULL, '', ','),
                    IFNULL(`mob2`, ''), IF(`mob2` IS NULL, '', ','),
                    IFNULL(`mob3`, ''), IF(`mob3` IS NULL, '', ','),
                    IFNULL(`mob4`, ''), IF(`mob4` IS NULL, '', ','),
                    IFNULL(`mob5`, ''), IF(`mob5` IS NULL, '', ','),
                    IFNULL(`mob6`, ''))
                 AS mobile
      FROM `temp_consignordata`) AS T
Rulisp
  • 1,586
  • 1
  • 18
  • 30
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 03 '22 at 10:42
0
SELECT item_code, 
   -- First if
   IF(category_code = 'HERR1', 1, 
   -- second else IF
    IF(category_code = 'COLN5', 2,
   -- last else
   'NO') 
 AS category_code, 
 item_name, 
 item_quantity 
FROM qa_items;

Explanation

first if evalutes for value 'HERR1' and if found assigns 1
Second (else ) IF evalues for Value 'COLN5' and if found assigns 2
Last (else) default case assigns 'NO'
to category_code