Here is my table structure. and I want to get records between provided date range.
|--------------|---------------------|
| id | year | month |
|--------------|---------------------|
| 1 | 2015 | 01 |
| 2 | 2015 | 02 |
| 3 | 2016 | 02 |
|--------------|---------------------|
I tried with following query, but didn't work.
return $this->db->select('*')
->from('tourist T')
->where('T.year >=', $where['s_year'])
->where('T.month >=', $where['s_month'])
->where('T.year <=', $where['e_year'])
->where('T.month <=', $where['e_month'])
->get()->result();
Try This:
$this->db->select('*')
->from('test T')
->where('T.year >='.$where['s_year'].' and T.month >='.$where['s_month'])
->where('T.year <='.$where['s_year'].' and T.month <='.$where['s_month'])
->get()->result();
Related
I have a table prices with where I store more times in a day more values referred to a customer like this:
Table prices:
| id | customer_id | value_1 | value_2 | value_3 | created_at |
-------------------------------------------------------------------------
| 1 | 10 | 12345 | 122 | 10 | 2021-08-11 10:12:40 |
| 2 | 10 | 22222 | 222 | 22 | 2021-08-11 23:56:20 |
| 3 | 12 | 44444 | 444 | 44 | 2021-08-12 08:12:10 |
| 4 | 10 | 55555 | 555 | 55 | 2021-08-13 14:11:20 |
| 5 | 10 | 66666 | 666 | 66 | 2021-08-13 15:15:30 |
| 6 | 10 | 77777 | 777 | 77 | 2021-08-13 16:12:50 |
I have some filters on that table to retrieve only records with date greater than X and/or lower than Y, sort records by value_1 or value_2, etc...
With that filters I have to take only 1 record for each day of a customer specified.
I'm able to get the record with the highest value_1 for example, by using sql function max() and group by date.
// Init query
$query = Price::query();
// Take the greatest value of value1
$query = $query->selectRaw(
'max(value_1) as highest_value_1, ' .
'date(created_at) as date'
);
// If defined, add a greater or equals
if ($from) $query->where("created_at", ">=", $from);
// If defined add a lower or equals
if ($to) $query->where("created_at", "<=", $to);
// Get results for current customer only, grupping by date and ordering it
$query = $query->where('customer_id', $id)->groupBy('date')
->orderBy('date', 'DESC');
// Fetch records
$records = $query->get();
But now I would like to have only the last record for each day of a customer specified.
I need an eloquent/sql solution because the date range to search may be large and the table has a lot of records.
How can I archive that?
Thanks
Not a complete solution (no customer filter nor laravel use), but I would use something like that in pure sql :
SELECT
CONCAT(EXTRACT(YEAR_MONTH FROM created_at),EXTRACT(DAY FROM created_at)) AS day,
MAX(created_at)
FROM mytable
GROUP BY day;
Of course, you may use other function to group by day (like string regexp or substr).
I'm tryng to join two tables in my Laravel controller code, and view them in one Datatable.
table1
+--------------------+---------+
| recordtime | tempout |
+--------------------+---------+
| 4.12.2020 10:00:00 | 1.1 |
| 4.12.2020 10:30:00 | 1.2 |
| 4.12.2020 11:00:00 | 1.3 |
| 4.12.2020 11:30:00 | 1.4 |
| 4.12.2020 12:00:00 | 1.5 |
+--------------------+---------+
table2
+--------------------+---------+
| recordtime | tempout |
+--------------------+---------+
| 4.12.2020 10:00:00 | 2.1 |
| 4.12.2020 11:00:00 | 2.3 |
| 4.12.2020 12:00:00 | 2.5 |
| 4.12.2020 13:00:00 | 2.6 |
| 4.12.2020 14:00:00 | 2.7 |
+--------------------+---------+
When I use this code:
$results = Tablemodel1::whereBetween('table1.recordtime', $dateScope)
->selectRaw('table1.tempout,table2.tempout as tempoutstamb,table2.recordtime')
->leftJoin('table2', function($join){
$join->on('table1.recordtime', '=', 'table2.recordtime');
})
->orderBy('table1.recordtime', 'ASC')
->get();
return Datatables::of($results)
->make(true);
It's giving me all records that equals to the coresponding recordtime and with aditional records that are on every half hour but with null(invalid date) values from table1. How to display their date instead of null(invalid date)?
+--------------------+---------+--------------+
| recordtime | tempout | tempoutstamb |
+--------------------+---------+--------------+
| invalid date | 1.2 | - |
| invalid date | 1.4 | - |
| 4.12.2020 10:00:00 | 2.1 | 1.1 |
| 4.12.2020 11:00:00 | 2.3 | 1.3 |
| 4.12.2020 12:00:00 | 2.5 | 1.5 |
+--------------------+---------+--------------+
added working Laravel query based on #miken32 answer:
$results2 = Tablemodel1::whereBetween('table1.recordtime', $dateScope)
->selectRaw('table1.recordtime')
->selectRaw('max(table1.tempout) as tempout')
->selectRaw('max(table2.tempout) as tempoutstamb')
->leftJoin('table2', function($join){
$join->on('table1.recordtime', '=', 'table2.recordtime');
})
->groupBy('table1.recordtime');
$results = Tablemodel2::whereBetween('table2.recordtime', $dateScope)
->selectRaw('table2.recordtime')
->selectRaw('max(table1.tempout) as tempout')
->selectRaw('max(table2.tempout) as tempoutstamb')
->leftJoin('table1', function($join){
$join->on('table1.recordtime', '=', 'table2.recordtime');
})
->groupBy('table2.recordtime')
->orderBy('recordtime', 'ASC')
->union($students2)
->get();
Here's some SQL that does the trick:
SELECT table1.recordtime, table1.tempout, table2.tempout AS tempoutstamb
FROM table1
LEFT JOIN table2 ON (table1.recordtime = table2.recordtime)
UNION
SELECT table2.recordtime, table1.tempout, table2.tempout AS tempoutstamb
FROM table2
LEFT JOIN table1 ON (table1.recordtime = table2.recordtime)
ORDER BY recordtime
You're looking for a full join, but MySQL doesn't do those. So we fake it with a UNION query.
For use in Laravel, probably easiest to just wrap the whole thing in a raw statement.
I use of codeigniter(php) and have a table in database i want select query equal words no whatever it looks like it is.
For example I have the following table in the database MySQL:
I want get row that have num 2 of Active column.what do i do?
+----------------------------------------+
| Name Active Insert_Time |
+----------------------------------------+
| mon 2222 1481203712 |
| tue 2 1481202788 |
| wed 222 1481202581 |
| thu 22 1479902588 |
+----------------------------------------+
I've tried like this:
$search_term = $this->input->post('search');
$this->db->select('Active, Name', FALSE);
$this->db->where('Active LIKE', '%'.$search_term_num.'%');
$query = $this->db->get('submit_house');
this query give me all rows, but I'd just row 2
Use $this->db->get_where():
$query = $this->db->get_where('database_table', array('Active' => 2));
Source:
https://www.codeigniter.com/userguide3/database/query_builder.html
I have below table
+--------------+-----------+
| serial no | isNew |
+--------------+-----------+
| 630401A | 2014 |
| 630401B | 2014 |
| 630401A | 2013 |
| 630401B | 2013 |
| 630401C | 2013 |
+--------------+-----------+
Now the thing is I want to select records as per isNew column. As you can see that serial no is same and isNew is different in 2 cases and in on case there is only record for serial number.
So I want output like
+--------------+-----------+
| serial no | isNew |
+--------------+-----------+
| 630401A | 2014 |
| 630401B | 2014 |
| 630401C | 2013 |
+--------------+-----------+
I hope I am very clear to display my table and result I want.
Thanks for your help in advance!
select * from TABLENAME group by serialno order by isNew DESC
select `serial no`, max(isNew) from table group by `serial no`
GROUP BY statement is used in conjunction with the aggregate functions to group the result-set
Use group by serialno
select * from table group by serialno order by isNew DESC
How can I get the month interval of a transaction from the last date of it's record to the current date?
Let's look at the table below!
---------------------------------------
|transaction_dates | transaction_time |
---------------------------------------
|2015-01-02 | 11:00:00 |
|2015-01-03 | 12:00:00 |
|2015-02-05 | 7:00:00 |
|2015-03-05 | 10:00:00 |
|2015-03-05 | 19:00:00 |
|2015-04-05 | 13:00:00 |
---------------------------------------
From the current date, August 20, 2015. I don't have any transaction for the month of May,June and July. So by doing some PHP script and SQL, how can I get the months wherein transaction becomes idle to the current date? In the above case, my desired output would be
-------------
|months_idle|
-------------
|5 |
|6 |
|7 |
--------------
Create table with months (1..12) in your DB and select all month > max(MONTH(transaction_dates)) and < MONTH(NOW())