MySQL execute SELECT based on condition - mysql

How to use CASE appropriately for executing SELECT query based on user input ?
On executing following query :
SELECT
CASE WHEN user_input_variable = 'y' THEN
(SELECT * FROM table_foo WHERE bar = '6f322766-0ec0-4d24-840f-c857a82a6efe')
ELSE
(SELECT 0)
END
If the user has selected 'y' then it should return records from the table, else it should return an empty result set.
I am getting error:
Operand should contain 1 column(s)

The sub-query must return not more than 1 column. Do a UNION ALL instead.
Note that the number of columns must be the same, that's why you need to select null's in the second select.
SELECT * FROM table_foo WHERE bar = '6f322766-0ec0-4d24-840f-c857a82a6efe')
and user_input_variable = 'y'
union all
SELECT 0, null, null... where user_input_variable <> 'y'

You could use the IF condition also.
IF user_input_variable = 'y'
SELECT * FROM table_foo WHERE bar = '6f322766-0ec0-4d24-840f-c857a82a6efe')

Related

How to run two different queries based on IF condition?

I have a scenario in which a table might exist in some databases. I need my query to run when the table exists and also when it doesn't. The below query when ran, gives an error Operand should contain 1 column(s). How can I make this query work without using a stored procedure? I need to use it with PySpark SQL.
SELECT IF(
EXISTS(
SELECT * FROM information_schema.TABLES WHERE TABLE_SCHEMA = 'dbdemo' AND TABLE_NAME = 'tier_report') > 0,
(SELECT a.*, b.tierchangedate, b.tierchangetype
FROM
(SELECT mobile, enrolledstorecode as storecode, tier as customertier, isprofileupdated, DATE(profileupdatedate) as profileupdatedate, modifiedenrolledon as enrolledon, referralcode, (CASE WHEN(countrycode IS NULL) THEN '91' ELSE countrycode END) as countrycode
FROM dbdemo.member_report) a
INNER JOIN
(SELECT mobile, DATE(tierchangedate) as tierchangedate, tierchangetype FROM dbdemo.tier_report) b
ON a.mobile=b.mobile),
(SELECT mobile, enrolledstorecode as storecode, tier as customertier, isprofileupdated, DATE(profileupdatedate) as profileupdatedate, modifiedenrolledon as enrolledon, referralcode, (CASE WHEN(countrycode IS NULL) THEN '91' ELSE countrycode END) as countrycode
FROM dbdemo.member_report));

how to use user defined variables in where clause

I have this query, and i m trying to use the user defined variable #noVar in my where clause to show only the records with value 'Yes' on that variable.
but when I use Having #noVar = 'Yes' as in the query below, it returns 0 result.
SELECT svcreqdetail.id, svcreqcheckin.stime as checkin, #etime:= time(timestampadd(minute, svcreqdetail.hours*60 , concat(caredate,' ', caretime))) as endtime, svcreqcheckout.stime as checkout, time_to_sec( if(svcreqcheckout.stime > svcreqcheckin.stime,
timediff(svcreqcheckout.stime, svcreqcheckin.stime),
addtime(timediff(svcreqcheckout.stime, svcreqcheckin.stime), '24:00:00.000000')))/3600 AS wrkHrs, svcreqdetail.hours,
svcreqstatus.status, #checkoutvar:= time_to_sec(timediff(svcreqcheckout.stime, #etime))/60 as checkoutvar,#noVar:= if (#checkoutvar <= 15,'Yes', 'No') as noVar, qualif
FROM svcreqdetail
LEFT JOIN svcreqcheckin ON svcreqcheckin.reqid = svcreqdetail.id
LEFT JOIN svcreqcheckout ON svcreqcheckout.reqid = svcreqdetail.id
JOIN svcreqstatus ON svcreqstatus.reqdid = svcreqdetail.id
WHERE (yearweek( caredate ) = yearweek( date_sub( CURRENT_DATE, INTERVAL 1 week ) )
AND svcreqstatus.status != 'Incompleted'
AND svcreqstatus.status != 'Deleted')
having #noVar = 'Yes'
is there anyway i can test against that variable in my where clause. and thank you
I don't think you can use user-defined variables in the HAVING clause like that.
One option would be to put your existing query (without the HAVING clause) into a sub-query and filter the results using a WHERE clause outside the sub-query, like so:
SELECT *
FROM
(
<your existing query goes here>
) AS sub_query
WHERE noVar = 'Yes'

MySQL where clause that should not work but returns all table contents

This query is returning all the table contents. To me is not a valid where clause. Anyone have any idea why this works?
SELECT * FROM TableName WHERE 1
In MySQL, TRUE is a costant value = 1, while FALSE is = 0, your query is then eqivalent to:
SELECT * FROM TableName WHERE TRUE
also, all conditions are converted either to 0 or to 1:
SELECT 'a' = 'a'
will return 1, while
SELECT 'a' = 'b'
will return 0 so for example the following queries are all equivalent:
SELECT * FROM TableName WHERE TRUE
SELECT * FROM TableName WHERE 'a' = 'a'
SELECT * FROM TableName WHERE 1
but every value <> 0 is considered true as well, so even this will return all rows:
SELECT * FROM TableName WHERE 2
but if a value <> is considered true, one would expect the following query to work:
SELECT * FROM TableName WHERE 2 = TRUE
but this won't return anything, because 2 = 1. Yes, sometimes MySQL is a little weird.
Select * from TableName where 1 is equivalent with Select * from TableName because it mens where true

Where mistake in sql LIKE clause?

I try to count all items from another table with this select:
SELECT id, name, (SELECT count(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
and prekes_main.pg_kodas LIKE 'grupes_main.pg_kodas%') as pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
and grupes_main.name <> ''
In LIKE clause I want automatically get selected grupes_main column pg_kodas, but in this query it always returns 0, where is mistake in LIKE function? thx
SELECT id, name,
(
SELECT COUNT(*)
FROM prekes_main
WHERE prekes_main.pristKaina = 1
AND prekes_main.pg_kodas LIKE CONCAT(grupes_main.pg_kodas, '%')
) pristKaina
FROM grupes_main
WHERE grupes_main.level = 1
AND grupes_main.name <> ''

Handling empty set in MySQL CASE statement

MySQL server version 5.0.45. Consider the following:
( SELECT CASE
WHEN t.group_id = 12 THEN 'yes'
ELSE 'no'
END
FROM sample_table t
WHERE t.user_id = 2
AND t.group_id = 12 ) as foo
This subquery of a larger statement works as I'd expect, yielding a 'yes' or 'no' string value most of the time. It's not ideal, but that's what you get for working on someone else's code!
However, sometimes the select can legitimately return an empty set, in which case foo is set to NULL. This does not actually break the application, but it's irritating. Is there a way I can guarantee that foo will always be either 'yes' or 'no', even if there are no matching rows in the table?
If it's a scalar subquery, (i. e. you use in a SELECT or WHERE clause, not in FROM clause), then use this:
IF(
EXISTS
(
SELECT NULL
FROM sample_table t
WHERE t.user_id = 2
AND t.group_id = 12
), 'yes', 'no'
)
or even this:
COALESCE(
(
SELECT 'yes'
FROM sample_table t
WHERE t.user_id = 2
AND t.group_id = 12
), 'no')
If you want the empty set to represent a "no" case, you could probably do this instead:
select ... where ... (foo = 'no' or foo is null)
This would handle both casees without having to drastically change your subquery.