Check which condition failed in complex sql query statement - mysql

I want to know which condition from where clause of SQL query fails.
For example I have below statement
SELECT * from Users where status != 'inactive'
AND ptype != 'test' AND ptype != 'test1'
AND (p_name NOT IN ('ptest', 'ptest0', 'ptest1', 'ptest2') OR p_name IS NULL)
AND (p_m_c NOT LIKE '%pmcestt%'
OR (ptype LIKE 'SomeOthertest%' AND p_m_c IS NULL)
OR (TType = 'Broad' AND p_m_c IS NULL)
OR p_m_c IN ('pmctest', 'pmctest0', 'pmctest1', 'pmctest2', 'pmctest3',
'pmctest4', 'pmctest5', 'pmctest6', 'pmctest7', 'pmctest8'))
How can I know which condition failed, and get that condition?

If you want to check conditions you will have to move condition statement in WHERE clause to CASE statement for each condition in SELECT list apart from columns of table Users.
SELECT *,
CASE WHEN status !='inactive' THEN 1 else 0 end as statusTag,
CASE WHEN ptype !='test' THEN 1 ELSE 0 end as ptypeTag,
.
.
CASE WHEN p_m_c IN ('pmctest', 'pmctest0', 'pmctest1', 'pmctest2', 'pmctest3',
'pmctest4', 'pmctest5', 'pmctest6', 'pmctest7', 'pmctest8') THEN 1 else 0 end as p_m_cTag
FROM Users
This will generate list of columns for each table row indicating value 1 if condition is satisfied or 0 if condition is false for that particular row.

Related

On DUPLICATE KEY, Conditional statement error ambiguous

INSERT INTO tb1 (title,Family,Complexity)
Select o.title,o.Family,o.Complexity from tb2 o
ON DUPLICATE KEY UPDATE
title = CASE WHEN title <> o.title THEN o.title ELSE title END,
Family = CASE WHEN Family <> o.Family THEN o.Family ELSE Family END,
Complexity = CASE WHEN Complexity <> o.Complexity THEN o.Complexity ELSE Complexity END ;
I am trying to update tb1 with the records in tb2, ONLY if the records do not match up, as I am using an after update trigger in another table.
However when trying to execute this, I get an error "Column 'title' in field list is ambiguous".
I have been unable to solve this. Please assist
Try with full table name
INSERT INTO tb1 (title,Family,Complexity)
Select o.title,o.Family,o.Complexity from tb2 o
ON DUPLICATE KEY UPDATE
tb1.title = CASE WHEN tb1.title <> o.title THEN o.title ELSE tb1.title END,
tb1.Family = CASE WHEN tb1.Family <> o.Family THEN o.Family ELSE tb1.Family END,
tb1.Complexity = CASE WHEN tb1.Complexity <> o.Complexity THEN o.Complexity ELSE tb1.Complexity END ;
This is because the title field exists in multiple tables/references.
You should prefix your title with the table name or it's reference.

Need a single SELECT query in php

I am having three parameters $page, $date, $search. i need a single select sql query where i have to check all the three variables one by one whether any of these variables is having some value or not or if all of the three variables is having some value. According to which it will match from the database and give the result. Please help me out.
You can make where clause as follows in your Select query.(Below is MS SQL format, you can modify CASE statement equivalent with MY SQL)
SELECT * FROM TABLE (if more than one table, you can use join)
WHERE TABLE.search_COLUMN = case when #search <> '' then #search else TABLE.search_COLUMN end
AND
TABLE.page_value_COLUMN = case when #page_value <> '' then #page_value else TABLE.page_value_COLUMN end
AND
TABLE.date_‌​value_COLUMN = case when #date_‌​value <> '' then #date_‌​value else TABLE.date_‌​value_COLUMN end

SELECT STATEMENT IN A CASE STEMENT INSIDE A SELECT STATEMENT

I am trying to display a field value based on the value of field and then find a external table record.
can I do it?
SELECT
CASE
WHEN (dsp_notes IS NOT NULL) THEN '*'
WHEN (dsp_notes IS NULL) THEN ''
ELSE ''
END,
CASE
WHEN (dsp_priority = '1') THEN [SELECT uvi_value FROM PUB.universalinfo WHERE uvi_key = 'DSP01SHORT']
Is this possible?
Yes. This is called a scalar subquery and it needs to return one column and one row:
(CASE WHEN dsp_priority = '1'
THEN (SELECT ui.uvi_value FROM PUB.universalinfo ui WHERE ui.uvi_key = 'DSP01SHORT')
END) as NewCol
I strongly encourage you to use table aliases on your column references.

IF Then Else Mysql

Working on a view that pulls from two table however in one table I need to select either one field or another depending on a third..it's the if else that has me stubbed.
Create view as
select
pens.PartNo,
pens.Title,
ranges.weight
if(pens.SpeacialOffer = 1 then pens.offer as Price else pens.Price)
from
pens, ranges
where
pens.penrange = ranges.id;
If the specialoffer is falged the the view needs to pull in the offer else it needs to pull in the Price.
What you need is a CASE operator:
CASE
WHEN condition
THEN value_a
ELSE value_b
END
So in your case:
CASE
WHEN pens.SpeacialOffer = 1
THEN pens.offer
ELSE pens.price
END
This replaces the entire column definition in your SELECT statement, so the whole view becomes:
Create View as
Select
pens.PartNo,
pens.Title,
ranges.weight,
Case
When pens.SpeacialOffer = 1
Then pens.offer
Else pens.price
End as Price
From
pens, ranges
Where
pens.penrange = ranges.id;
Use CASE, also converted the query to explicit join instead of implicit join
select pens.PartNo,
pens.Title,
ranges.weight,
(Case when
pens.SpeacialOffer = 1 then
pens.offer else pens.Price
end ) as Price
FROM pens,
JOIn ranges
ON pens.penrange = ranges.id;
Here's one way:
Create view as select
pens.PartNo,
pens.Title,
ranges.weight,
(pens.SpeacialOffer * pens.offer + (1 - pens.SpeacialOffer) * pens.price) as Price
from
pens,
ranges
where
pens.penrange = ranges.id;

How to perform if else statement within Where clause Sql Server 2008

I'm working on the following simple query. I want to innerjoin squad s and group g tables but with condition (#ZeroOrOne parameter will be 0 or 1)
WHERE
CASE WHEN IsNumeric(#ZeroOrOne) = 0 THEN
s.TeamId=g.Team1Id --if #ZeroOrOne value is 0 then perform this statement
ELSE
s.TeamId=g.Team2Id --if #ZeroOrOne value is 1 then perform this statement
What is the right way; should I change my query or logic?
Try this.
WHERE
s.TeamId = CASE WHEN IsNumeric(#ZeroOrOne) = 0 THEN g.Team1Id
ELSE g.Team2Id END
OR you can also use this in ON clause
JOIN Table s
ON s.TeamId = CASE WHEN IsNumeric(#ZeroOrOne) = 0 THEN g.Team1Id
ELSE g.Team2Id END