replacing a column value with a constant in codeigniter select query - mysql

I have a query in codeigniter like this $this->db->select('t.*,m.last_traded_price online_curr_nav, m.actual_change days_change')
now in place of m.actual_change, i have to pass a constant say "NA" to days_change. PLease tell how can i do that.

You might want this:
$this->db->select('t.*,m.last_traded_price online_curr_nav,
m.actual_change days_change, "NA" as days_change')

Related

property of non-object to change value to 0

I have a curious question that's going on my mind while looking at my datatables.
I noticed that some columns of my table has no values, like just the column only.
Is it possible when you want to call that column it will return a value of 0?
I tried to call it with a simple SELECT query in sql but its giving me a notice.
Notice: Trying to get property of non-object MySQL result on line (number line)
Can this be used with an if-else statement from the query or a case statement from the query? Hoping for your opinions on this. Thank you :)
Try to use some function like NVL() in Oracle, or IFNULL() in MySQL. This functions set a value when you got a NULL value.

Want to replace a Id value in a column in data in SQL

I have a parameter like
#strbody=id:XXX sub:YYY asd:ZZZ date1:1611031729 date2:1611031729
I want to use a SQL command to replace only the ID value in #strbody.
Can anyone help me on the same.
I think this will work
#strbody = CONCAT('id:', #newid, SUBSTRING(#strbody, LOCATE('sub:', #strbody)));

Using results in one query to use in another query - Orace MySql

I want to be able to use the results of the first query and place into the second query, sorry if this doesn't make much sense, im new to all of this.
First Query
SELECT "NAME", "TYPE","CATEGORY" "Meters" FROM BBT_Locations
WHERE SQRT(Power((:myLocX-LOCX), 2) + Power((:MyLocY - LOCY),2)) < :Distance;
Second Query
Select Round((:Distance/20)*4)as "Meters";
Can you please elaborate more as in which field value of 1st query you would like to use in 2nd query ? There are JOIN query and Sub query concept in SQL which can be used. But we should know the field that we would like to use and get value for, based on that we can select the kind of query to write.

Creating an OR statement using existing conditions hash

I am working on a problem where I need to add an OR clause to a set of existing conditions. The current conditions are built in a hash in a method and at the end, they are used in the where clause. Here is a simplified example:
...
conds.merge!({:users => {:archived => false}})
Model.where(conds)
I am trying to add an OR clause to the current set of conditions so it would be something like '(conditions) OR new_condition'. I'd like to add the OR statement without converting each addition to the conds hash into a string. That would be my last option. I was hoping someone has done something like this before (without using Arel). I seem to recall in Rails 2 there was a way to parse a conditions hash using a method from the model (something like Model.some_method(conds) would produce the where clause string. Maybe that would be a good option to just add the OR clause on to that string. Any ideas are appreciated. Thank you for your help!
I found a way to do what I needed. Instead of changing all of the conditions that I am building, I am parsing the conditions to SQL using sanitize_sql_for_conditions. This is a private method in ActiveRecord, so I had to put a method on the model to allow me to access it. Here is my model method:
def self.convert_conditions_hash_to_sql(conditions)
self.sanitize_sql_for_conditions(conditions)
end
So, once I convert my conditions to text, I can add my OR clause (along with the appropriate parentheses) to the end of the original conditions. So, it would go something like this:
Model.where('(?) OR (model.type = ? AND model.id IN(?))', Model.convert_conditions_hash_to_sql(conds), model_type, model_id_array)

Dynamic Values in 'IN' operator

I have a select statement in which the WHERE clause's IN operator. The query works properly as long as some values are passed to question mark (passed from java program). But when there are no values passed, I get a syntax error.
select
this_.categoryAddressMapId as category1_1_0_,
this_.categoryId as categoryId1_0_,
this_.addressId as addressId1_0_
from
icapcheckmyphotos.category_address_map this_ <br>
where
this_.addressId in (
?
)
When there are no parameters passed, I need null set. Please suggest how to modify the where clause. Thanks in advance
Modify your java program. Your choices are to not run the query if there are no addressIds, or ensure that it passes at least one value. If addressId is an integer field, pass -1 or something like that. Personally, I like the first option but it depends on your requirements.
how about doing sometiong like
where (? is null) OR this_.addressId in ( ? )
you may need to add some special treatment to the first part of the OR if your columns does accept NULLS
//pseudo code
...
WHERE 1
if(!null) {
AND this_.addressId in ('stuff')
}