using conditional statement in WHERE clause mysql - php

If keep_base_amount is null then I want to use country_id=236 in where clause, else I want to use keep_base_amount's value as in country_id.
I have query something like this:
SELECT * FROM account_treasury_local
WHERE
CASE WHEN keep_base_amount IS NOT NULL THEN country_id = keep_base_amount
ELSE country_id = '236' END
There is a record in database. But, I am getting nothing in result. Is there anything missing/wrong in above query.

As simple as this
SELECT * FROM account_treasury_local
WHERE (keep_base_amount IS NOT NULL AND country_id = keep_base_amount) OR (keep_base_amount IS NULL AND country_id = '236')

Have you tried:
SELECT * FROM account_treasury_local
WHERE
(keep_base_amount IS NOT NULL AND country_id = keep_base_amount)
OR
(keep_base_amount IS NULL AND country_id = '236')
I'm not sure I'm understanding your question correctly.

Related

Codeigniter db->where_in adding ''

I am having a problem with where_in . I am trying to get the shop name which possess the lookbook had the specific point id
$this->db->select('shop');
$this->db->from('shopify_lookbook');
$this->db->where_in('lookbook_id', 'SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid');
The problem is the query it generate
SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN('SELECT lookbook_id FROM shopify_point WHERE point_id = 543')
It will give blank but when I try in mysql without '' in IN() like below
SELECT `shop` FROM `shopify_lookbook` WHERE `lookbook_id` IN(SELECT lookbook_id FROM shopify_point WHERE point_id = 543)
It returns the shop name that I want. How can I erase '' in $this->db->where_in()
You might use where instead and to construct your IN clause there:
$this->db->where('lookbook_id IN (SELECT lookbook_id FROM shopify_point WHERE point_id = $pointid)', NULL, FALSE);

if condition in where clause in MySQL+ PHP

I have a variable in PHP. I want to check my PHP variable in MySQL query and if it is not null use it in a where clause.
select * from T1
where post = "news" and $city = cityname and $city is not null
$city is PHP variable.
I have a problem when $city is null, that should show all news posts but it returns nothing.
this is my table:
Since $city is a PHP variable, if it is NULL then when you echo it in your query you will simply get nothing. That will make an invalid query; it will look like this:
select * from T1
where post = "news" and = cityname and is not null
To make this work, you need to enclose $city in your query in quotes, and then rather than comparing it to NULL, compare it to the empty string i.e.
select * from T1
where post = "news" and ('$city' = cityname or '$city' = '')
Note that the correct logical operator is or for this use case.
As was pointed out in the comments, you should look into prepared statements. This question has some really useful information: How can I prevent SQL injection in PHP?
Try this
select * from T1
where post = "news" and ($city = cityname or $city is not null )

mysql query for selecting data in multiple columns

I have table with 10 columns and I want to check input value in where clause of the MySQL query.
I want to do something like this. But, when I use this query I am getting an error.
for example :
SELECT * FROM user_data
where poll_title='$poll_title'
and '$voter' IN (user_vote_1,user_vote_2,user_vote_3...user_vote_10)
order by idpoll ASC
user_vote_1 to 10 (value is null'ed in the database) and I want to retrieve only that rows from a column which have $voter value.
I think you need this comparison (Not Sure OfCourse) :-
SELECT * FROM user_data
where poll_title = "$poll_title"
and (user_vote_1 = "$voter"
OR user_vote_2 = "$voter"
OR user_vote_3 = "$voter"
OR user_vote_4 = "$voter"......OR user_vote_10 = "$voter")
order by idpoll ASC
If I've understood what you want to do - return only the column with the value - then would coalesce do the job? This assumes that the value in user_vote_n will either match the value you're looking for or be null, since coalesce returns the first non-null argument.
(untested)
select coalesce(user_vote_1, user_vote_2, user_vote_3, ) as UserVote from user_data
where coalesce(user_vote_1, user_vote_2, user_vote_3, ) = '$voter';
That aside, this looks like a structure that could do with normalising - a single 'user_vote' column and a single 'user_vote_number' column.

Query based on enum value

I am trying to query based on the value of an enum field. Essentially I have 3 values (categories) for the enum field, a, b and c respectively. I am attempting to populate a select field based on the results of the following query:
SELECT * WHERE pid = $id AND group = 'a';
I have tried this with and without single quotes and get the error You have an error in your SQL syntax; and have narrowed it down to being a lack of knowing how to query based on a specific enum value. I assumed this would work and see no reason for it not to so if I can be enlightened I would greatly appreciate it.
Group is a reserved keyword use it like this
SELECT * FROM mytable WHERE pid = $id AND `group` = 'a';
Use tablename
SELECT * From tablename WHERE pid = $id AND `group` = 'a';
Try This..
SELECT * FROM tbname WHERE pid = $id AND `group` = 'a';

Mysql JoinInner if not null

Hi i have following query where it's use joininner statement to get all possible businesses. But when a business is created for first time only 1 category will be updated the rest 2 will remain null
public function searchBusinessByCategoryString($str = null, $city=null,$start,$perpage)
{
$select = $this->getDbTable()->getAdapter()->select();
$select->from('business as b', array('b.business_name','b.business_url','b.reviews_num','b.cat_id','b.business_id','b.rating','b.business_phone','b.business_add1','b.business_add2','b.x','b.y','b.photo_url'))
->joinInner('business_category as bc','b.cat_id = bc.cat_id',array('bc.cat_name'))
->joinInner('business_sub_category as bsc','b.sub_cat_id = bsc.b_sub_cat_id',array('bsc.b_subcat_name','bsc.b_sub_cat_id'))
->joinInner('business_sub_category as bsc2','b.sub_cat2_id = bsc2.b_sub_cat_id',array('bsc2.b_subcat_name','bsc2.b_sub_cat_id'))
->joinInner('business_sub_category as bsc3','b.sub_cat3_id = bsc3.b_sub_cat_id',array('bsc3.b_subcat_name','bsc3.b_sub_cat_id'))
->where("bsc.b_subcat_name like '".$str."%'")
->orWhere("bsc.b_subcat_name like '%".$str."'")
->orWhere("bsc.b_subcat_name= '".$str."'")
->orWhere("bsc2.b_subcat_name like '%".$str."'")
->orWhere("bsc2.b_subcat_name = '".$str."'")
->orWhere("bsc2.b_subcat_name like '".$str."%'")
->orWhere("bsc3.b_subcat_name like '%".$str."'")
->orWhere("bsc3.b_subcat_name = '".$str."'")
->orWhere("bsc3.b_subcat_name like '".$str."%'");
$result = $this->getDbTable()->getAdapter()->fetchAll($select);
return $result;
}
Now the issues is how can i not doing joininner query if the rest 2 categories are null? My above statement return empty result event though there is businesses with one category.
use leftJoin instead of innerJoin where the joined table can contain NULL value. INNER JOIN will join table, using the condition and will not keep lines when a null value is found on the joined table. LEFT JOIN will allow you to keep this line

Categories