Table invitation (Primary key: invitation_id)
---------------------------------------------
invitation_id send_user_id receive_user_id
---------------------------------------------
1 2 1
2 3 2
3 2 7
4 9 2
---------------------------------------------
If send_user_id value is 2 then I need to get receive_user_id and if receive_user_id is 2 then I need to get send_user_id
How to get this ?
Use a CASE expression:
SELECT invitation_id,
CASE WHEN send_user_id = 2 THEN receive_user_id
WHEN receive_user_id = 2 THEN send_user_id
ELSE -1 END AS output
FROM invitation
Note that I am returning -1 should neither of the two conditions be true, but we can easily replace this with other logic if you provide it.
try using nested if else if you have multiple conditions to check
if // your condition
begin
//enter you code
end
else if //2nd condition
begin
// your code
end
else //default condition if none of the above execute
begin
//default code
end
SELECT invitation_id,
CASE
WHEN receive_user_id = 2 THEN send_user_id
WHEN send_user_id = 2 THEN receive_user_id
END AS output
FROM INVITATION.
May be its give proper answer.
Format code properly.
Related
So I got 2 columns whereby they have different values but are dependent. I want to find those that are not showing the correct values. I really am stumped about what to do.
SELECT *
Let's take those value ends with html should have a value of 3 in the answer column,py should have a value of 4 in the answer column, jpg should have a value of 5 in the answer column and Null should reflect Null in the answer column
webpage
answer
abc.html
3
xyz.py
4
qrs.html
2
syt.jpg
5
NULL
1
Result should show
webpage
answer
qrs.html
2
NULL
1
You can use CASE to check for all those possible combinations. For example:
select t.*,
case
when webpage like '%.html' then
case when answer = 3 then 'VALID' else 'INVALID' end
when webpage like '%.py' then
case when answer = 4 then 'VALID' else 'INVALID' end
when webpage like '%.jpg' then
case when answer = 5 then 'VALID' else 'INVALID' end
when webpage is null then
case when answer is null then 'VALID' else 'INVALID' end
else 'Unrecognized webpage'
end as validation
from t
EDIT:
If you only want to show the invalid ones then you can do:
select *
from (
select t.*,
case
when webpage like '%.html' then
case when answer = 3 then 'VALID' else 'INVALID' end
when webpage like '%.py' then
case when answer = 4 then 'VALID' else 'INVALID' end
when webpage like '%.jpg' then
case when answer = 5 then 'VALID' else 'INVALID' end
when webpage is null then
case when answer is null then 'VALID' else 'INVALID' end
else 'Unrecognized webpage'
end as validation
from t
) x
where validation <> 'VALID'
SELECT
*
FROM
your_table
WHERE
(webpage LIKE '%.html' AND answer <> 3)
OR (webpage LIKE '%.py' AND answer <> 4)
OR (webpage LIKE '%.jpg' AND answer <> 5)
OR (webpage IS NULL AND answer IS NOT NULL)
You can try using CASE to check in WHERE condition:
SELECT *
FROM test
WHERE CASE WHEN webpage LIKE '%html' AND answer=3 THEN 1
WHEN webpage LIKE '%py' AND answer=4 THEN 1
WHEN webpage LIKE '%jpg' AND answer=5 THEN 1
WHEN webpage IS NULL AND answer IS NULL THEN 1
ELSE 0 END = 0
Fiddle
I'm creating a table to store rules for another selects and it has the following design:
id
column
operator
value
next_rule_id
next_operator
1
mail
contains
#gmail.com
2
OR
2
mail
contains
#hotmail.com
5
AND
3
city
equals
NY
5
AND
4
mail
contains
jolie#
3
AND
5
city
equals
NY
4
null
6
user_id
greater
10
null
null
I need to select 1 rule id and return all rows based on next_rule_id
Here are some examples:
selecting rule id = 1 would return
1 mail contains #gmail.com 2 OR
2 mail contains #hotmail.com 5 AND
5 city equals NY 4 null
selecting rule id = 4 would return
4 mail contains jolie# 3 AND
3 city equals NY 5 AND
5 city equals NY 4 null
selecting rule id = 6 would return
6 user_id greater 10 null null
SOLUTION 1
I've managed to do that with a temporary table and one loop. But I think there is better approaches to do that.
If anyone knows how to do that in a better way I would appreciate your help.
SOLUTION 2
I created this procedure that uses a loop too.
BEGIN
SET #id = 1; -- hardcoded first id
SET #rules = '';
SET #next_id = NULL;
loop1: LOOP
SET #count = #count + 1;
SELECT next_rule_id INTO #next_id FROM filter_rules WHERE id = #id;
SET #rules = CONCAT(#rules, ',', #id);
SET #id = #next_id;
IF #id IS NULL THEN
LEAVE loop1;
END IF;
END LOOP loop1;
SELECT * FROM filter_rules WHERE FIND_IN_SET(filter_rules.id, #rules);
END
I have a MySQL database with a table named generations the structure of the table is as follows
I want to get value 10 as output when ten_generation have value 1 otherwise it will not return any value, 20 as output if twenty_generation have value 1 otherwise it will not return any value, 30 as output if thirty_generation have value 1 otherwise it will not return any value. If all the three fields has a value 1 output will be 10,20,30 also the task_id will provided as the input.
Its unclear what you intend the output to be when multiple generation columns are 1 but one solution is to use a CASE statement:
SELECT CASE
WHEN ten_generation = 1 THEN 10
WHEN twenty_generation = 1 THEN 20
WHEN thirty_generation = 1 THEN 30
ELSE NULL
END AS value
FROM generations
WHERE id = :your_id
If you want it as multiple columns then:
SELECT CASE
WHEN ten_generation = 1
THEN 10
ELSE NULL
END AS ten_value,
CASE
WHEN twenty_generation = 1
THEN 20
ELSE NULL
END AS twenty_value,
CASE
WHEN thirty_generation = 1
THEN 30
ELSE NULL
END AS thirty_value
FROM generations
WHERE id = :your_id
if only twenty_generation contain value 1 the output is 20 and if twenty_generation and ten_generation contain value 1 output is 10,20
Oracle Query:
SELECT TRIM(
LEADING ',' FROM
CASE WHEN ten_generation = 1 THEN '10' END
|| CASE WHEN twenty_generation = 1 THEN ',20' END
|| CASE WHEN thirty_generation = 1 THEN ',30' END
) AS value
FROM generations
WHERE id = :your_id
For MySQL you'd use CONCAT_WS:
select
concat_ws(',',
case when ten_generation = 1 then '10' end,
case when twenty_generation = 1 then '20' end,
case when thirty_generation = 1 then '30' end
) as result
from mytable
where task_id = 2;
I've the following code
select * from weeks
where case
when wet>1000 then wenumber=1
when wet>500 then wenumber=2
when wet>100 then wenumber=3
else wenumber= 22
end
it gives me the result of both ( when > 1000 and else )
through my search I understood that it is search case, but
I need to make it a simple case
simply if the first condition were true stop evaluating "else" statement
any help
Better way of doing it
select *
from weeks
where ( wet>1000 and wenumber = 1 ) or wenumber = 22
Try this:
SELECT *
FROM weeks
WHERE wenumber = CASE WHEN wet > 1000 THEN 1
WHEN wet BETWEEN 500 AND 1000 THEN 2
WHEN wet BETWEEN 100 AND 500 THEN 3
ELSE 22
END;
You need to specify which column should be compared with result of CASE like:
SELECT *
FROM weeks
WHERE wenumber = (CASE
WHEN wet > 1000 THEN 1
ELSE 22
END)
In my database (MySQL) table, has a column with 1 and 0 for represent true and false respectively.
But in SELECT, I need it replace for true or false for printing in a GridView.
How to I make my SELECT query to do this?
In my current table:
id | name | hide
1 | Paul | 1
2 | John | 0
3 | Jessica | 1
I need it show thereby:
id | name | hide
1 | Paul | true
2 | John | false
3 | Jessica | true
You have a number of choices:
Join with a domain table with TRUE, FALSE Boolean value.
Use (as pointed in this answer)
SELECT CASE WHEN hide = 0 THEN FALSE ELSE TRUE END FROM
Or if Boolean is not supported:
SELECT CASE WHEN hide = 0 THEN 'false' ELSE 'true' END FROM
I got the solution
SELECT
CASE status
WHEN 'VS' THEN 'validated by subsidiary'
WHEN 'NA' THEN 'not acceptable'
WHEN 'D' THEN 'delisted'
ELSE 'validated'
END AS STATUS
FROM SUPP_STATUS
This is using the CASE
This is another to manipulate the selected value for more that two options.
You can do something like this:
SELECT id,name, REPLACE(REPLACE(hide,0,"false"),1,"true") AS hide FROM your-table
Hope this can help you.
If you want the column as string values, then:
SELECT id, name, CASE WHEN hide = 0 THEN 'false' ELSE 'true' END AS hide
FROM anonymous_table
If the DBMS supports BOOLEAN, you can use instead:
SELECT id, name, CASE WHEN hide = 0 THEN false ELSE true END AS hide
FROM anonymous_table
That's the same except that the quotes around the names false and true were removed.
You can use casting in the select clause like:
SELECT id, name, CAST(hide AS BOOLEAN) FROM table_name;
I saying that the case statement is wrong but this can be a good solution instead.
If you choose to use the CASE statement, you have to make sure that at least one of the CASE condition is matched. Otherwise, you need to define an error handler to catch the error. Recall that you don’t have to do this with the IF statement.
SELECT if(hide = 0,FALSE,TRUE) col FROM tbl; #for BOOLEAN Value return
or
SELECT if(hide = 0,'FALSE','TRUE') col FROM tbl; #for string Value return
in Postgres 11 I had to do this:
type is an int
SELECT type,
CASE
WHEN type = 1 THEN 'todo'
ELSE 'event'
END as type_s
from calendar_items;
replace the value in select statement itself
(CASE WHEN Mobile LIKE '966%' THEN (select REPLACE(CAST(Mobile AS nvarchar(MAX)),'966','0')) ELSE Mobile END)