changing sql statement to eloquent query - sql

So I have an SQL statement as such;
AND created_at BETWEEN '20190601' and '20190630'
How do I change this to an eloquent query? Or is it better to just use a db:raw query?
All inputs are very much appreciated. Cheers!

You can try this :
$yourQuery->whereBetween('created_at', ['20190601', '20190630']);
Read more at whereBetween section in here

As you are comparing dates, then do this:
$fromDate = '20190601';
$toDate = '20190630';
$yourquery->whereBetween('created_at', array($fromDate->toDateTimeString(), $toDate->toDateTimeString()) )->get();

Related

How to convert SQL Query to CodeIgniter?

How to convert this query :
SELECT pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah
FROM pembelian_detail_tb
INNER JOIN produk_tb ON pembelian_detail_tb.kode_produk = produk_tb.kode_produk;
I have try many code, and i still got undefine.
Try this
$query = $this->db->select('pembelian_detail_tb.kode_beli, pembelian_detail_tb.kode_produk, produk_tb.nama, pembelian_detail_tb.jumlah')
->from('pembelian_detail_tb')
->join('produk_tb', 'pembelian_detail_tb.kode_produk = produk_tb.kode_produk', 'inner')
->get();
I like to use Query Bindings once a join is involved in a query:
https://www.codeigniter.com/userguide3/database/queries.html
search page for "query bindings"
this escapes values for you of course and if you need to debug dump $this->db->last_query();

? in ActiveRecord select

In a where statement, you can use variables like:
Order.where('employee_id = ?', params[:employee_id])
I'm trying to accomplish something similar with a select, but it's not working:
Order.select('amount FROM line_items WHERE employee_id = ? AS employee_line_items', params[:employee_id])
=> ERROR: syntax error at or near "1"
=> LINE 1: ...ployee_id" = ? AS employee_line_items, 1
What's going on here? Is it possible to use ? in select statement? If not, how can you insert an escaped sql string here? I'd like to just use #{params[:employee_id]}, but this bit of code would be vulnerable to sql injection.
You have to split your query and chain it:
Order.select('amount FROM line_items').where(['WHERE employee_id = ?', params[:employee_id]])
And also based on this question, I believe you cannot use AS in WHERE clause, only when selecting fields (and you can't use them in WHERE in any case)
Check the documentation to understand how select works on ActiveRecord models

Need help converting SQL query to Ruby.

I'm new to Ruby on Rails. I'm trying to determine the proper ruby query for the following SQL query.
Select max(bid_amount) from biddings where listing_id = 1;
I need to extract the maximum value in the bid_amount column. But it has to have a dynamic listing_id.
Try:
Bidding.where('listing_id = :listing_id', listing_id: 1).maximum(:bid_amount)
Update:
To follow up on your comment: since you say you are passing in params[:id], it's best to convert that parameter to integer so that unwanted values don't go to the database. For e.g.
Bidding.where('listing_id = :listing_id', listing_id: params[:id].to_i).maximum(:bid_amount)

Convert PostgreSQL to SqlAlchemy query

I have a simple query in PostgreSQL which I need to convert to SQLAlchemy.
SELECT table1.name,table2.ip,table2.info
FROM table1 LEFT OUTER JOIN table2
ON(table1.name=table2.hostname)
GROUP BY table1.name;
I have tried using this with no success:
session.query(table1.name
,table2.ip
,table2.info).distinct().join(table2).group_by(table1.name)
Can someone please help me with this?
Thanks in advance
Ishwar
Here's how you would do it using the Expression API
http://docs.sqlalchemy.org/en/latest/core/expression_api.html
Define your table, for example:
table1 = table('table1',
column('name', String())
)
table2 = table('table2',
column('ip', String()),
column('info', String()),
column('hostname', String()),
)
Compose your query, like:
my_query = select([table1.c.name,
table2.c.ip,
table2.c.info]
).select_from(
table1.outerjoin(table2, table1.c.name == table2.c.hostname)
).group_by(table1.c.name)
Execute your query, like:
db.execute(my_query)
It's not clear from your answer what the problem is, but my shoot at this query would be:
session.query(table1.name
,table2.ip
,table2.info)\
.outerjoin(table2)\
.group_by(table1.name)
I figured out the answer. I was actually missing the ON condition in query:
session.query(table1.name
,table2.ip
,table2.info
).distinct().outerjoin(table2 , table1.name=table2.hostname
).group_by(table1.name)
Thanks for help!

Doctrine Query <timestamp

I have a colum "datetime", like this: 2012-06-04 15:40:20.
I want to create a query in Doctrine that I get the data of previous time. Less than 2012-06-04 15:40:20. How can I realize that in Doctrine.
Sorry, I just have no clue.
If I understand your question correctly, I believe the syntax is just:
$datetime = // your timestamp
->where('t.somefield < ?', date('Y-m-d H:i:s', strtotime($datetime))
I am not familiar with Doctrine, but here is standard SQL to do what you want:
select *
from t
where t.datetime in (select max(datetime)
from t
where datetime < '2012-06-04 15:40:20'
)
If Doctrine supports standard SQL syntax, then something like this would work (you might have to input the date/time constant in a different way).