Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7687623
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T19:44:40+00:00 2026-05-31T19:44:40+00:00

I would like to extract a value or its inverse from database. I would

  • 0

I would like to extract a value or its inverse from database. I would like to find a way to do the if statement in SQL, not in PHP.

My table looks like : id, rate, from_value, to_value

I have $from_value and $to_value variables, and would like to extract the corresponding rate value or its inverse.

I would like to

SELECT `rate` if `from_value` = $from_value and `to_value` = $to_value

or

SELECT (1/`rate`) if `from_value` = $to_value AND `to_value` = $from_value 

with only one request.

I have searched a lot of ways to do that with CASE, IF, but I didn’t find a working solution.
I probably missed something. Thanks

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T19:44:41+00:00Added an answer on May 31, 2026 at 7:44 pm

    Here is a portable method using CASE. I have it returning NULL if neither condition is met.

    SELECT 
      CASE
        WHEN `from_value` = $from_value AND `to_value` = $to_value THEN rate
        WHEN `from_value` = $to_value AND `to_value` = $from_value THEN (1.0 / rate)
        ELSE NULL
      END AS rate
    FROM tbl
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to extract the GPS EXIF tag from pictures using php. I'm
I would like to extract from a general HTML page, all the text (displayed
I would like to extract items from this sample html, more specificly, i would
Using Php I would like to extract the current Url of a page including
I have a full MS SQL Backup file that I would like to extract
Hello! I would like to extract all citations from a text. Additionally, the name
I would like to extract the date a jpg file was created. Java has
I would like to extract portion of a text using a regular expression. So
I have the following snippet where I would like to extract code between the
In a TImage's OnClick event, I would like to extract the x,y coordinates of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.