property of non-object to change value to 0 - mysql

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.

Related

Laravel SQL is not performing as expected. No syntax error

Can any Laravel developers help me understand why the following SQL statement is not working. I don't get any errors, it simply doesn't do what it should:
DB::table('devices')
->leftJoin('hotel_device','hotel_device.device_id','devices.id')
->where("hotel_device.device_id","IS","NULL")
->delete();
This should remove all device ids where the leftJoin link returns as NULL (ie. where this device id is not being used in the "hotel_device" table).
If I run it as raw SQL directly in the database it works correctly. Note the "IS" condition rather than "=" condition, as I am refering to a NULL value. (using "=" didn't find any matching rows)
thanks.
You should use the whereNull method to compare to null rather than trying to filter it using the regular where method.
DB::table('devices')
->leftJoin('hotel_device','hotel_device.device_id','devices.id')
->whereNull("hotel_device.device_id")
->delete();

Limit Results from mysql using if statement

I've got if statement code that works for the where clause of mysql query, but not for the limit statement. Could someone please help me to get this to work or a similar alternative? It gives me an expected result Boolean error.
I'm trying to use the if statement to find out if the $limit variable has any value except OFF. If it does, then limit the results of the mysql query by whatever number, 20 in this case. I'm not sure if that helps.
I honestly don't know if this the correct way of doing this.
$limit='20'; or whatever number
IF('$limit'<>'OFF',LIMIT $limit,'$limit'=0)
you could avoid if using a ternary operator
$limit='20'; or whatever number
$limit_result = ($limit<>'OFF' ? $limit : 0);

replacing a column value with a constant in codeigniter select query

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')

Replace IIf with SWITCH in WHERE clause

I have the following query in MS-Access 2003 and it works OK:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (tblDiscounts.DiscountID) = IIf ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2,IIf([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3,4)));
I wish to replace the IIf function with the Switch function but whatever I tried didn't work. My best approach is the following:
SELECT tblDiscounts.DiscountID, tblDiscounts.DiscountPercent, tblDiscounts.DiscountName, tblDiscounts.DiscountDescription
FROM tblDiscounts, qryPropertyPeriodRate_Count_Nested
WHERE (((tblDiscounts.DiscountID)=SWITCH ([qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=1,1, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=2,2, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]=3,3, [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]>3,4)));
but I get a message
Type mismatch in expression
Please advise!
One difference I can see is that if [qryPropertyPeriodRate_Count_Nested].[CountOfWeeks]<1 the nested IIfs will return 4 while the Switch statement will return Null. Check your underlying data to see if that could happen; a Null value might very well mess up the WHERE clause.

Query is not returning proper value

As the part of my Query question i am trying to write a sql query which is returning null and zero value how to resolve this please see it.
In your supplied SQL Fiddle, you need to remove the single quotes around:
'user_messages.messageid'
to just be:
user_messages.messageid
The first won't actually match the results.
Updated SQL Fiddle