If inside Where mysql - mysql

Can I do an if inside Where?
or something that allows me to do the checks only if the field is not null (path=null)
SELECT
IF(path IS NOT NULL, concat("/uploads/attachments/",path, "/thumbnails/" , nome), "/uploads/attachments/default/thumbnails/avatar.png") as avatar_mittente
FROM prof_foto  
WHERE profilo_id = 15
 -- only if path != "/uploads/attachments/default/thumbnails/avatar.png"
AND foto_eliminata = 0 AND foto_profilo = 1

You can try this
SELECT
IF(path IS NOT NULL, concat("/uploads/attachments/",path, "/thumbnails/" , nome), "/uploads/attachments/default/thumbnails/avatar.png") as avatar_mittente
FROM prof_foto
WHERE profilo_id = 15
AND IF( path != "/uploads/attachments/default/thumbnails/avatar.png",
foto_eliminata = 0 AND foto_profilo = 1,
foto_eliminata like '%' AND foto_profilo like '%'
)

You can just use binary logic for this:
SELECT * FROM TABLE WHERE A
(if b then also where C)
Becomes
SELECT * FROM TABLE WHERE A
AND (!b OR C)
In your case:
SELECT
IF(path IS NOT NULL, concat("/uploads/attachments/",path, "/thumbnails/" , nome), "/uploads/attachments/default/thumbnails/avatar.png") as avatar_mittente
FROM prof_foto
WHERE profilo_id = 15
AND (path = "/uploads/attachments/default/thumbnails/avatar.png" OR foto_eliminata = 0 AND foto_profilo = 1))

Yes, you can use IF() wherever a value is expected, even though most of the time it is not the best route to take.
WHERE IF(path IS NULL, portfolio_id = 15, TRUE)
But prefer:
WHERE path IS NULL OR portfolio_id = 15
(notice you cannot compare with NULL, [anything] = NULL is always FALSE)

Related

MySQL to filter result by using where & and condition

I'm trying to fetch some rows from a question table in MySQL like this:
SELECT *
FROM raise_question
where documentID = '1'
and delReq != 1 OR delReq IS NULL
However, some rows have different documentID. (Please see the picture). Is this a correct way (using HAVING instead of AND) to return the result I want ( which is it only contains specific documentID):
SELECT *
FROM raise_question
where documentID = '1'
having delReq != 1 OR delReq IS NULL
With AND query returns:
The expected result (only contains one documentID) after using Having I want is like this:
Your WHERE translate to
where (documentID = '1'
and delReq != 1) OR delReq IS NULL
, because AND has higher precedence than OR
Put bracket in your OR statement.
where documentID = '1'
and (delReq <> 1 OR delReq IS NULL)
Is this what you want?
select distinct documentid
from raise_question
where delReq <> 1 or delReq is null;
This returns document ids that have a non-1 delreq value.

MySQL function IFNULL is not working correctly with Wildcards

I have a SQL Script where I am using wildcard in If Null statement.
I am looking to show all other records regardless of field has null values in it.
SELECT * FROM ABC WHERE (Value=1 AND
Prefix like IFNULL(${parameter:X},'%')
AND
Base like IFNULL(${parameter:Y},'%')
AND
Suffix like IFNULL(${parameter:Z},'%')
AND
Area like IFNULL(${parameter:Ax},'%')
);
I am expecting the result should show all values regardless if Area filed value is null
Don't use like. It will filter out NULL values in the data. Just do:
WHERE (NewestVersion = 1 AND
(Prefix like ${param:PrefixLookup} or ${param:PrefixLookup} is null) AND
(Base like ${param:BaseLookup} or ${param:BaseLookup} is null) AND
(Suffix like ${param:SuffixLookup} or ${param:SuffixLookup} is null) AND
(Custom2 like ${param:DRELookup} or ${param:DRELookup} is null)
If you have empty strings, then comparisons to NULL will not help. Just use:
WHERE (NewestVersion = 1 AND
(Prefix like ${param:PrefixLookup} or ${param:PrefixLookup} = '') AND
(Base like ${param:BaseLookup} or ${param:BaseLookup} = '') AND
(Suffix like ${param:SuffixLookup} or ${param:SuffixLookup} = '') AND
(Custom2 like ${param:DRELookup} or ${param:DRELookup} = '')

Exclude column in where clause if field value is empty

I have a stored procedure for search screen where I have 5 different filters.
Users can leave the filters empty or have one filter and click on search button.
All the data is coming from database, I am trying to create stored procedure which can exclude columns in where clause if the parameter value is empty string.
#Id as nvarchar(256) = 1
#blnIsinProgress bit = 0
#strStatus varchar(20) = ''
#strName varchar(50) = ''
#strConfirmationNumber varchar(50) = 123
#dtmSubmittedFrom Date = '12/31/9999'
#dtmSubmittedTo Date = '12/31/9999'
as
BEGIN
SELECT *
FROM tblTable
WHERE
(#Id IS NULL OR lngID = #Id) AND
(#blnIsinProgress IS NULL OR blnIsinProgress = #blnIsinProgress) AND
(#strStatus = '' OR strStatus = #strStatus) AND
(#strName= '' OR strName= #strName) AND
(#strConfirmationNumber = '' or #strConfirmationNumber = #strConfirmationNumber )
End
But
Execute spManage 1,0,'','',123
will give me all the results
The problem is this line:
(#strConfirmationNumber = '' or #strConfirmationNumber = #strConfirmationNumber )
The second condition is always true. You have an extra #. So, try this:
(#strConfirmationNumber = '' or #strConfirmationNumber = strConfirmationNumber )

How to use null||d.AM_Answer for select query

I'm getting data from database and using union for joining 2 table. But I need to use null || d.AM_Answer. Here I'm using only null, null. But it's taking only null value coming. If I stored the answer, I'm not getting answer. So, I need to use null || d.AM_Answer.
select
b.QM_ID, b.QM_QCM_ID, b.QM_Question, b.QM_Type, b.QM_Parent_Id, null, null
from
question_master b
INNER JOIN Assessment_master d
on ( d. AM_QM_ID = b.QM_Parent_Id
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
and d.AM_ASM_Local_Id = %# )
You can use ifnull to check if a field is null or not. If not null, then use field value.
Example:
IFNULL( d.AM_Answer, null )
This returns value of d.AM_Answer if it is not null.
And, you have some errors in your value comparison statements.
AND d.AM_HNM_ID = %d
AND d.AM_HM_ID = %d
AND d.AM_ASM_Local_Id = %#
If %d and %# are place holders for numerics, then it is wrong way of input.
For runtime value inputs, you have to use ? place holder and use prepared statements to bind values on those parameters.
AND d.AM_HNM_ID = ?
AND d.AM_HM_ID = ?
AND d.AM_ASM_Local_Id = ?
Use your server side scripting language for value binding.
Refer to:
MySQL: IFNULL(expr1,expr2)

WHERE Clause only IF NOT NULL

I need help with my SELECT.
I got a field that can be NULL and in it there is stored a foreign-key.
SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerBrandID = beerID <--- this can be NULL
So if it's NULL nothing happens.
How can I check if beerID is NOT NULL so I can use "beerBrandID = beerID"?
You probably need something like this:
First example:
SELECT * FROM beerImage WHERE beerBRewID = brewID AND (beerID IS NULL OR beerBrandID = beerID)
Second Example:
SELECT * FROM beerImage WHERE beerBRewID = brewID AND beerID IS NOT NULL AND beerBrandID = beerID
The first example will allow you to show records which have beerID null along with the beers which beerBrandID is equal to beerID (both).
The second one will return exactly beers which correspond to beerBrandID (excluding NULL beerIDs).
How about using with CASE statement in where clause like
WHERE
CASE WHEN beer.id IS NOT NULL
THEN beer.brand_id = 12345
ELSE TRUE
END
If you want to include records where there's no match, you need an outer join
SELECT beer.id AS beerID,
beer.barrelID AS barrelID,
beer.imageID AS imageID,
beer.title AS title,
beer.map AS map,
beer.longitude AS longitude,
beer.latitude AS latitude,
brand.name AS brandName,
brew.type AS brewType,
image.name AS imageName,
variation.name AS variationName
FROM brand, brew, image, beer
LEFT OUTER JOIN variation ON variation.ID = beer.VariationID
WHERE beer.id = %s
AND md5(beer.memberID) = %s
AND beer.review = 1
AND brand.ID = beer.brandID
AND brew.ID = beer.brewID
AND image.ID = beer.imageID
To check for null / not null, use IS NULL / IS NOT NULL
AND beerID IS NOT NULL
You can use "IS NULL" or "IS NOT NULL" MySQL comparison functions.
Read more about this here:
http://dev.mysql.com/doc/refman/5.0/en/working-with-null.html
http://dev.mysql.com/doc/refman/5.0/en/comparison-operators.html#operator_is-null