Query is not returning proper value - mysql

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

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

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.

Mysql where clause syntax

I was running through some of our code at work and came across this structure for an sql query and was sure it was a typo in the variables of our PHP, I ran it and it works.
Select column from table where value = column;
Anyone I know was always taught the correct syntax is:
Select column from table where column = value;
Is there any reason for this being legal, other than SQL just checks are both sides of an equation equal to each other?
I'm more posting this because I found it really interesting, like a 'the more you know' kind of thing.
The equality operator (=) is symmetric - if a=b is true, then so is b=a. a and b can be column names, values or complex expressions. It's common to use the form column=value, but syntactically speaking, it's completely equivalent to value=column.
Yes, you have it right sql just checks both sides of an equation. The equation could even contain a column on neither side! such as
SELECT column from table where 1=2;
The syntax of an SQL query is :
SELECT what_to_select
FROM which_table
WHERE conditions_to_satisfy;
You can use any condition you want, for example :
SELECT * FROM table WHERE 1
will return you all the rows
while
SELECT * FROM table WHERE 0
will return you nothing
after a WHERE you must have a condition, nothing else

Run query on query results

I'm trying to clean all duplicate results on a phonebook i have, that means i'm trying to compare landline1 to landline2 and if they are equal (but not empty) i would like to either NULL or just replace with empty string one of them.
i use the following line to list all the matching results:
SELECT * FROM `csv_confirmed` WHERE landline1=landline2 AND landline1!="";
which gives me a full list, but it's too many to edit manually and i'm trying to automate it.
What would be the easiest way to run UPDATE (or anything else that might work here) to clear the "landline2" column of the results i found ?
Just change your query to an update:
UPDATE csv_confirmed
SET landline2 = NULL
WHERE landline1=landline2 AND landline1!=""