SQL Case Basics -


i'm new in sql world wonder if have logic

if name john , if id john john 2

if id john john 1

i'm trying figure out if sql code make work is

case when "id" 'john a' 'john b'

when "id" 'john a' 'john 2" when "name" 'john' end

is of correct? better sql in case basis? appreciated

a quick test using sql server on rextester: http://rextester.com/glp50097

create table t (id varchar(32), name varchar(32)) insert t values   ('john a','steve') ,('john a','john') ,('joan a','joan') 

query:

select      id   , name   , case        when name = 'john'           , id = 'john a'          'john 2'       else name       end expr t 

returns:

+--------+-------+--------+ |   id   | name  |  expr  | +--------+-------+--------+ | john | steve | steve  | | john | john  | john 2 | | joan | joan  | joan   | +--------+-------+--------+ 

Comments

Popular posts from this blog

javascript - Confirm a form & display message if form is valid with JQuery -

Retrieving ETA (estimated time of arrival) with Google Distance Matrix API and public transit as transport mode -

ionic framework - Meteor - Error: Failed to execute 'insertBefore' on 'Node' -