SQL Like Statement is showing all results - mysql

I have used LIKE statements before and have been spending ages rewriting the statement and not sure what I have done wrong. When the query is ran, it displays all records in the database when it should be showing a more narrow list.
The reason for using a LIKE statement is to make my advanced search facility more efficient by allow part of a "property name".
SQL Statement:
SELECT
*
FROM
properties
WHERE
PropertyName LIKE '%$PropertyName%'
OR PropertyLocation LIKE '%$PropertyLocation%'
OR PropertyType LIKE '%$PropertyType%'
OR PropertyBeds='$PropertyBeds'
OR PropertyRate >= '$PropertyRate1'
AND PropertyRate <= '$PropertyRate2'
Please note: The statement does work without using like and wildcards.

Your conditions are:
WHERE PropertyName LIKE '%$PropertyName%' or
PropertyLocation LIKE '%$PropertyLocation%' or
PropertyType LIKE '%$PropertyType%' or
PropertyBeds = '$PropertyBeds' or
PropertyRate >= '$PropertyRate1' and PropertyRate <= '$PropertyRate2'
If PropertyName, PropertyLocation, or PropertyType are empty strings, then you will return all the rows. That is my first guess on what is happening.
Perhaps you want AND as a connector rather than OR.

Related

SQL Sum Case Statement - group by case

I'm simply trying to write an sql query to sum a group of fields.
I have 2 columns, one of which is Account Name, the other is Amount
I'd only like to sum two of the account names:
=sum(case when [Account Name] like Salaries or 'Other Income', Fields!Amount.Value)
the following works fine,
=sum(Fields!Amount.Value)
but I'm trying to narrow down the number of account names that are included in the sum. I'm trying to figure out how to use a case statement then group by that case statement.
I'm going to take a stab in the dark here because I think from your example that you're using SQL Server Reporting Services (SSRS), and the syntax is quite different to T-SQL or ANSI SQL, which is what most people think of when you say "SQL".
If that's the case, and I have no experience with this so this may be completely wrong, I'm guessing you might need to try something using either iif or switch like this:
=sum(iif(Fields![Account Name].Value = 'Salaries'
or Fields![Account Name].Value = 'Other Income', Fields!Amount.Value, 0))

MySql explode/in_array functionalilty

In my table I have a field with data such as 1,61,34, and I need to see if a variable is in that.
So far I have this
SELECT id, name FROM siv_forms WHERE LOCATE(TheVariable, siteIds) > 0
Which works, with the exception that if the siteIds were 2,61,53, and TheVariable was 1, it would return the row as there is a 1 in 61. Is there anyway around this using native MySql, or would I need to just loop the results in PHP and filter the siteIds that way?
I've looked through the list of string functions in MySql and can't see anything that would do what I'm after.
Try with find_in_set function.
SELECT id, name FROM siv_forms WHERE find_in_set(TheVariable, siteIds);
Check Manual for find_in_set function.

Eloquent count distinct returns wrong totals

i'm having an issue with how eloquent is formulation a query that i have no access to. When doing something like
$model->where('something')
->distinct()
->paginate();
eloquent runs a query to get the total count, and the query looks something like
select count(*) as aggregate from .....
The problem is that if you use distinct in the query, you want something like
select count(distinct id) as aggregate from .....
to get the correct total. Eloquent is not doing that though, thus returning wrong totals. The only way to get the distinct in count is to pass an argument through the query builder like so ->count('id') in which case it will add it. Problem is that this query is auto-generated and i have no control over it.
Is there a way to trick it into adding the distinct on the count query?
P.S Digging deep into the builders code we find an IF statement asking for a field on the count() method in order to add the distinct property to the count. Illuminate\Database\Query\Grammars\BaseGrammar#compileAggregate
if ($query->distinct && $column !== '*')
{
$column = 'distinct '.$column;
}
return 'select '.$aggregate['function'].'('.$column.') as aggregate';
P.S.1 I know that in SQL you could do a group by, but since i'm eager loading stuff it is not a good idea cause it will add a IN (number of id's found) to each of the other queries which slows things down significantly.
I faced the exact same problem and found two solutions:
The bad one:
$results = $model->groupBy('foo.id')->paginate();
It works but it will costs too much memory (and time) if you have a high number of rows (it was my case).
The better one:
$ids = $model->distinct()->pluck('foo.id');
$results = $query = $model->whereIn('foo.id', $ids)->paginate();
I tried this with 100k results, and had no problem at all.
This seems to be a wider problem, discussed here:
https://github.com/laravel/framework/issues/3191
https://github.com/laravel/framework/pull/4088
Untill the fixes are shipped with one of the next Laravel releases, you can always try using the raw expressions, like below (I didnt test it, but should work)
$stuff = $model->select(DB::raw('distinct id as did'))
->where('whatever','=','whateverelse')
->paginate();
Reference: http://laravel.com/docs/queries#raw-expressions
$model->where('something')->distinct()->count('id')->paginate();

Two ways to select ranges in SQL, only one in MongoDB?

I have the following SELECT statement for SQL.
SELECT TransAmount FROM STOCK WHERE TransAmount between 100 and 110;
However, this statement generates an error from querymongo.com. It says "Failure parsing MySQL query: Unable to parse WHERE clause due to unrecognized operator ". I assume it is talking about the between clause.
Correct me if I'm wrong, but does this SQL statement do the exact same thing as the one above?
SELECT TransAmount FROM STOCK WHERE TransAmount > 100 and TransAmount < 110;
This statement generates the following MongoDB code.
db.STOCK.find({
"TransAmount": {
"$gt": 100,
"$lt": 110
}
}, {
"TransAmount": 1
});
It looks like MongoDB doesn't have a 'between' operator. Does MongoDB handle selection within ranges with a different keyword, or do you have to set it up like so $gt/%lt?
Between is just a shortcut (a sort of symlink) to your second query, I guess it makes life easier.
MongoDB has not yet implemented such a shortcut, I have looked around a bit for a JIRA declaring someone wants such an operator however, no luck.
The one and only way of doing ranges in MongoDB is to use $gt and $lt (you could count $in etc but that is a different kind of range, not what your looking for).

Condition check with IN clause mysql

I have a string returned from a function "'aa','bb','cc',..."(the function uses GROUP_CONCAT). I want to use this as a condition in the IN clase of mysql.
SELECT name,class,comment
FROM vwstudent v
WHERE name IN (select func())
I want the query to act like
SELECT name,class,comment
FROM vwstudent v
WHERE name IN ('aa','bb','cc','dd',..)
I assume ('aa','bb','cc','dd',..) is acting as a whole string here and isn't generating any result. What can I do to run this query error less.
I'm not sure if it'll help. But you might be able to hack something together by using PREPARE and EXECUTE as described here: How To have Dynamic SQL in MySQL Stored Procedure
... but that's completely new to me. I've never used anything like that in MySQL.