Datatables - where with two conditions - mysql

I used the following statement to filter records by subject ids. (Here subject_id=5)
$this->datatables->where('letter_letter.subject_id', 5);
That is working perfectly. Further I want to filter records in subject_id range like 1 to 10. Then I changed my code as follows :
$this->datatables->where('letter_letter.subject', 10, '<');
But did not get the desired output. How can I edit my code to get expected result ? Can anyone help me ?

Just use two calls to where() to define the range:
$this->datatables->where('letter_letter.subject_id >= ', 1);
$this->datatables->where('letter_letter.subject_id <=', 10);

This is one way you can make a where query with two conditions:
$this->datatables->where("letter_letter.subject_id IS ? AND letter_letter.subject BETWEEN ? AND ?", 5, 1, 10)

Related

Rails Active Record .paginate, Get First page(row), if page (row) specified doesn't exist in database table

In rails 4.2, I am using paginate method to get paginated records from db. I want to get first page, if specified page is not present in the db.
I am using following active record functionality to fetch paged records from database. So If the page number is mentioned 2 and there is only one row in db, the query returns null.
#records = #user.where("some where clause here").paginate(per_page: 1, page: params[:page].presence || 1).
I want to fetch first record if specified page doesn't exist in db. It fetches first record, if params[:page] is empty, but if database doesn't have specified page, it returns nill.
I think this is what you need:
#records = #user.where(title: 'bla').paginate(per_page: 1, page: params[:page].presence || 1).
#results = #records.empty? ? #user.where(title: 'bla').paginate(per_page: 1, page: 1) : #records
This will mean if your #records instance variable has any records it will be returned in #results, and if it doesn't your app will return the first page of results. To make it a bit more readable:
#first_page_records = #user.where(title: 'bla').paginate(per_page: 1, page: 1)
#custom_page_records = #user.where(title: 'bla').paginate(per_page: 1, page: params[:page].presence || 1)
#results = #custom_page_records.empty? ? #first_page_records : #custom_page_records
Although My answer is similar to one posted by Mark. But I still posted it, as it will run an extra query only if original query returns empty records.
#records = #users.valid_users_with_invalid_msg.paginate(per_page: 1, page: params[:page].presence || 1)
if #records.empty?
#records = #users.valid_users_with_invalid_msg.paginate(per_page: 1, page: 1)
end
Note: I have moved the where clause in scope

Access 2013 Calculation with If Statement

Maybe someone can help me. What I am trying to do is to create a calculation with an If Condition. I'm doing my current calculations in the field within my DesignerView.
=Summe([Menge/Liter])
=Summe([Menge/Liter]*[Preis/Liter])
Here i need to have that it only calculates the tables that have status = 1
Is there a way how to do this?
Use IIf Function:
=IIF(status = 1, Summe([Menge/Liter]*[Preis/Liter]), Summe([Menge/Liter] ))
Or is it:
=IIF(status = 1, Summe([Menge/Liter]*[Preis/Liter]), "")

Laravel count where row value > 0

I've been trying to find a solution for my query for some time now. But i haven't been able to solve it yet. Most of it works nicely, but the count part does not work like i intend it to.
My query looks like this:
$years = Sample::whereCostumerId($id)
->groupBy('year')
->orderBy('year', 'DESC')
->get(array(
DB::raw('year'),
DB::raw('COUNT(id) as antalProver'),
DB::raw('MIN(provnr) AS minProvnr'),
DB::raw('MAX(provnr) AS maxProvnr'),
DB::raw('count(P_HCl) AS numP_HCl'),
DB::raw('count(Total_lerhalt) AS numLerhalt'),
DB::raw('ROUND(AVG(pH),1) AS avgpH'),
DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
DB::raw('AVG(X) AS coordExist')
));
The issue here is that many of the rows in the column P_HCl and Total_lerhalt contains zero. And i don't want to count these. I only want to count where value is greater than zero.
Im shure there's some nice solution for this.
If you guys have any other solution for the query all together id be happy to see it.
Thanks
You‘re most of the way there—you just need to add a where clause to your query:
$years = Sample::whereCostumerId($id)
->groupBy('year')
->orderBy('year', 'DESC')
->where('year', '>', 0)
->get(array(
DB::raw('year'),
DB::raw('COUNT(id) as antalProver'),
DB::raw('MIN(provnr) AS minProvnr'),
DB::raw('MAX(provnr) AS maxProvnr'),
DB::raw('count(P_HCl) AS numP_HCl'),
DB::raw('count(Total_lerhalt) AS numLerhalt'),
DB::raw('ROUND(AVG(pH),1) AS avgpH'),
DB::raw('ROUND(AVG(P_AL),1) AS avgP_AL'),
DB::raw('ROUND(AVG(K_AL),1) AS avgK_AL'),
DB::raw('AVG(X) AS coordExist')
));

Sqlalchemy: Produce OR-clause with multiple filter()-Calls

I'm new to sqlalchemy and could use some help.
I'm trying to write a small application for which i have to dynamically change a select-statement. So I do s = select([files]), and then i add filters by s = s.where(files.c.createtime.between(val1, val2)).
This works great, but only with an AND-conjunction.
So, when I want to have all entries with createtime (between 1.1.2009 and 1.2.2009) OR createtime == 5.2.2009, I got the problem that i don't know how to achieve this with different filter-calls. Because of the programs logic it's not possible to use s= s.where(_or(files.c.createtime.between(val1, val2), files.c.createtime == DateTime('2009-02-01')))
Thanks in advance,
Christof
You can build or clauses dynamically from lists:
clauses = []
if cond1:
clauses.append(files.c.createtime.between(val1, val2))
if cond2:
clauses.append(files.c.createtime == DateTime('2009-02-01'))
if clauses:
s = s.where(or_(*clauses))
If you're willing to "cheat" by making use of the undocumented _whereclause attribute on Select objects, you can incrementally specify a series of OR terms by building a new query each time based on the previous query's where clause:
s = select([files]).where(literal(False)) # Start with an empty query.
s = select(s.froms).where(or_(s._whereclause,
files.c.createtime.between(val1, val2)))
s = select(s.froms).where(or_(s._whereclause,
files.c.createtime == datetime(2009, 2, 1)))
Building up a union is another option. This is a bit clunkier, but doesn't rely on undocumented attributes:
s = select([files]).where(literal(False)) # Start with an empty query.
s = s.select().union(
select([files]).where(files.c.createtime.between(val1, val2)))
s = s.select().union(
select([files]).where(files.c.createtime == datetime(2009, 2, 1)))

Why does my use of Perl's split function not split?

I'm trying to split an HTML document into its head and body:
my #contentsArray = split( /<\/head>/is, $fileContents, 1);
if( scalar #contentsArray == 2 ){
$bodyContents = $dbh->quote(trim($contentsArray[1]));
$headContents = $dbh->quote(trim($contentsArray[0]) . "</head>");
}
is what i have. $fileContents contains the HTML code. When I run this, it doesn't split. Any one know why?
The third parameter to split is how many results to produce, so if you want to apply the expression only once, you would pass 2.
Note that this does actually limit the number of times the pattern is used to split the string (to one fewer than the number passed), not just limit the number of results returned, so this:
print join ":", split /,/, "a,b,c", 2;
outputs:
a:b,c
not:
a:b
sorry, figured it out. Thought the 1 was how many times it would find the expression not limit the results. Changed to 2 and works.