SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Ternary operator in SQL?

    I am working on a search engine (see thread) for a university book store. I was having a problem with relevance but have found a solution (using fulltext searches) to almost everything problem except one:

    I would like to include a value that says if the column 'uni' is like the specified string then return a value of 2 otherwise 0. This will then be added to the sum of relvancy scores from other fields.

    i.e. I would like my total relevance field to be (where "mq" is the university code provided from the search form)

    Code:
    MATCH(title) AGAINST('blah')....+MATCH....+(uni LIKE '%mq%' ? 2 : 0) AS totalRelevance
    Is this possible in any way?

  2. #2
    SQL Consultant gold trophysilver trophybronze trophy
    r937's Avatar
    Join Date
    Jul 2002
    Location
    Toronto, Canada
    Posts
    39,341
    Mentioned
    63 Post(s)
    Tagged
    3 Thread(s)
    change this -- (uni LIKE '%mq%' ? 2 : 0)

    to this -- case when uni LIKE '%mq%' then 2 else 0 end
    rudy.ca | @rudydotca
    Buy my SitePoint book: Simply SQL
    "giving out my real stuffs"

  3. #3
    SitePoint Member
    Join Date
    Dec 2005
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks r937 - you're like a walking SQL dictionary


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •