I would need a SELECT statement which returns a Boolean value based on the result of two independent Boolean columns (connected with 'OR'). Does anybody have the syntax for this?
If your database supports boolean, then you can simply put the or expression in the select:
select t.*, (bool_col1 or bool_col2) as new_bool_col
from t;
Select case when (boolean condition) then column name from table name.
You can add multiple case condition to fetch the columns of conditional basis
in MySQL you can use IF() Function, like this:
SELECT IF(bool1 = 'true' OR bool2 = 'true', 'true', 'false')
FROM Yourtable;
Thanks, the comment of jarlh solved it: "Simply select col1 or col2 from tablename"
Related
How can I create a temp (empty) column in which values will be inserted upon logic:
SELECT ..., *temp_col*
FROM *table*
IF *table.column_1* is not null, THEN *temp_col=table.column_1*
else temp_col=table.column_2
Note: Colume_1 is a CLOB field which will be transformed to TEXT.
someone advised me to use the EXIST condition.
In SQL, you use the CASE expression or the COALESCE() function:
SELECT ...,
COALESCE(table.column_1, table.column_2) as temp_col
FROM table;
Using Case :
SELECT ...,
temp_col= CASE WHEN table.column_1 is not nul THEN table.column_1
ELSE
table.column_2 END
FROM table;
I will do a select statement inside my case statement like this:
CASE
WHEN d.dependent_speed_type = 4 THEN (SELECT column FROM tablename)
END
But this is not working.
Can I realize a select statement inside my case statement? If the value 4 is inside my column "d.dependent_speed_type" then it should be select a special value from another table. How is the right syntax?
You have to use something that will always return one value, like SELECT MAX(column) FROM tablename) for example.
I have a table which has a `department` column (allows null) but when I select that table and the field is null I don't want it to show Null but "-".
I'm told to put the if statement inside the select statement but I can't figure it out. How can I do this?
You want to use the function coalesce():
select coalesce(department, '-')
from table t
This is an ANSI standard function available in most databases.
You can use two methods:
1. Using CASE:
SELECT CASE WHEN department IS NULL
THEN '-'
ELSE department
END AS department FROM TableName
CASE evaluates a list of conditions and returns one of multiple possible result expressions. Read more here.
2. Using COALESCE:
SELECT COALESCE (department,'-') FROM TableName
COALESCE returns first parameter which is not null. Read more here.
You need to use the 'CASE WHEN' statement in your select query. Like this:
SELECT CASE WHEN Department IS NULL THEN Department = '-'
END AS DEPARTMENT
FROM Table_Name
You can use following code:
select ISNULL(department, '-') AS DEPARTM
from dbo.tbl_Department
i need such query
select * from t where
field=ifnull(:param, field) 'it not work's
so if param=NULL i have
select * from t where field is NULL
but if param =4
i have
select * from t where field=4
You can use the case when in where clause AFAIK bot not sure about MySQl,
But the better approach is to translate them,
you can read about that SQL WHERE clauses: Avoid CASE, use Boolean logic
So
select * from t where (:param is null and filed is null) or (filed = :param)
You can try this alternative
this might help you
select * from t where (field = NULL AND param= NULL) OR field ='4'
When working with NULL you cannot use arithmetic operators. Try COALESCE to make a logical if with values integer or NULL
mysql> SELECT COALESCE(NULL,1);
-> 1
mysql> SELECT COALESCE(NULL,NULL,NULL);
-> NULL
I think you are looking for NULLIF instead of ifnull
I think better approach would be to use CASE in where clause in your case.
For example i have requested:
WHERE (friend_id=? OR client_id=?)
How do i know which row meets friend_id condition and which meets client_id condition?
Is it possible to mark/flag rows depending of meeting condition somehow?
Thanks.
SELECT friend_id=? FROM yourtable WHERE (friend_id=? OR client_id=?);
You will get a true if the friend_id clause matches:
Or even:
SELECT friend_id=?, client_id=? FROM yourtable WHERE (friend_id=? OR client_id=?);
To get both matches. In this way you can see if one or both matches.
Use CASE operator
If knowing which row was hit because by any condition you can of course add this data to your result columns using case operators. The only downside is that your variables in your prepared statement (if that's what you're having here) are going to be doubled.
You can use UNION.
For example:
SELECT name, 1
FROM friends
WHERE friend_id=?
UNION
SELECT name, 0
FROM friends
WHERE client_id=?
Then when receiving data, you can check for that flag
SELECT *,
'Friend' AS Source
FROM TABLE
WHERE friend_id = ?
UNION
SELECT *,
'Client' AS Source
FROM TABLE
WHERE client_id = ?
But you'll have an issue if your entry is both friend and client. What do you want to happen in this case?
You can use IF(), but then you have to bind one of id twice:
SELECT IF(friend_id=?, 'friend', 'client') AS type
FROM table
WHERE (friend_id=? OR client_id=?)
you can add flags to the query:
SELECT *, IF((friend_id=$friend_id), 1, 0) AS friend_matched, IF((client_id=$client_id), 1, 0) AS client_matched FROM table WHERE (friend_id=? OR client_id=?)