Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
below is my database table, I need to select title xyz if category is fiction, for this I am using CASE condition but I am gettin error please anyone help me
------------------------------------------
     title         |     category
                   |     
-----------------------------------------

    xyz                  fiction

    adapt                fiction

    adaxx                 fiction

    follow                journal

    your                   acadamic
--------------------------------------------


<br />
SELECT  CASE WHEN category = fiction THEN title LIKE '%xyz%' END FROM books<br />


Its not working please help me.
Posted

1 solution

The query looks very odd. You can't build conditions in SELECT clause. For example the following would be a valid statement
SQL
SELECT CASE WHEN category = 'fiction' THEN 'found' END 
FROM books
WHERE title LIKE '%xyz%'

Also note that LIKE is an operator that can be used in WHERE clause, not much elsewhere.
 
Share this answer
 
Comments
Member 11406049 23-Feb-15 13:20pm    
will you tell me please what is 'found' in above query? and yes it's odd I am sorry, I am beginner...
Wendelius 23-Feb-15 13:34pm    
Your CASE statement inspects if the field category on a row contains a value 'fiction'. I wasn't sure what you wanted the result to be so I just added a constant literal 'found'. In other words for each row the select returns you get the text 'found' if the category is fiction. Is this something like you we're after or did you have something else in mind?
Member 11406049 23-Feb-15 13:43pm    
actually I have three columns title,author,fiction user may search by title name or author name by selecting the "category", if user search for 'adapt' its a 'title' name by selecting the "category fiction", I have to show 'adapt' and 'adaxx' because both are similar right, hence I used LIKE, another ex: if user is searching for 'xyz' by 'title' and by selecting "fiction category" I have to show result as 'xyz' is found...
Wendelius 23-Feb-15 14:04pm    
Can you post a small example, based on the data in the question, what would your conditions be and what would you expect as a result?
Member 11406049 23-Feb-15 14:07pm    
LINK

please check the above link, that is my previous post. and thank you very much for your replies..

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900