I want to check the value present in two fields of the table and based on that i want to display either field 1 or field 2. For Example:
Select name from users if address1='Mars'
else select age from users if address2='Mars';
I want to achieve this using plane sql because I need to implement this in the controller of rails using ActiveRecord::Base.connection.select_values("sql query")
What should be the SQL query to achieve the same. I could not find it anywhere. Thanks in advance.
use CASE
SELECT CASE
WHEN address1='Mars' THEN name
WHEN address2='Mars' THEN age
END
FROM users
请尝试User.find_by_sql("your sql query")
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.