Table inventory_month has three fields id , month_month and month_year
id month_month month_year
1 09 2012
2 10 2012
3 11 2012
4 01 2013
5 02 2013
6 03 2013
7 09 2013
8 10 2013
I need result FROM October 2012 TO October 2013 as below
Expected result
id month_month month_year
2 10 2012
3 11 2012
4 01 2013
5 02 2013
6 03 2013
7 09 2013
8 10 2013
But getting result below
Getting result
id month_month month_year
2 10 2012
8 10 2013
I have tried query below
select id,month_month,month_year where month_month>=10 AND month_year>=2012 AND month_month<=10 AND month_year<=2013
Need helpful suggestion, Thanks!
Use,
SELECT id,month_month,month_year WHERE
(month_month >= 10 AND month_year = 2012)
OR
(month_month <= 10 AND month_year = 2013)
Your condition logic is incorrect.
You need to replace your 2nd AND statement to an OR statement and add brackets.
(month_month>=10 AND month_year>=2012) OR (month_month<=10 AND month_year<=2013).
Hope this helps!
Try this
select id,month_month,month_year where (month_month>=10 AND month_year>=2012)
Or (month_month<=10 AND month_year<=2013)
Related
I'm working on a project that requires me to generate a report similar to this:
Category Total Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec
-----------------------------------------------------------------------
Category 1 97 10 10 10 10 10 5 10 10 7 5 5 5
Category 2 120 10 10 10 10 10 10 20 10 5 5 10 10
Category 3 5 0 0 0 3 0 2 0 0 0 0 0 0
This report needs to be generated from data similar to this:
id category_id type year month amount
----------------------------------------------------------
1 1 16 2016 1 4
2 1 23 2016 1 6
3 1 76 2016 2 3
4 1 27 2016 2 3
5 1 18 2016 2 4
In the report, each month's number is simply the sum of the amount for that particular category, year, and month. I can do this just fine by querying the database for each month and for each category, but this type of approach doesn't scale very well if I need to generate a report with about a hundred categories because it would take over 1,200 queries (100 categories X 12 months)!
Any suggestions of how to make this more efficient?
Thanks!
The solution is grouping by two columns.I added the necessary code to format the results as you want.
Item::where('year', $year)
->groupBy('month', 'category_id')
->selectRaw('sum(amount) as sum, month, category_id')
->orderBy('category_id')
->orderBy('month')
->get()
->groupBy('category_id')
->map(function($item){
$item = $item->pluck('sum', 'month');
$item['total'] = $item->sum();
return $item;
});
Is this what you're looking for?
Category::where('year', 2016)
->select('month', 'year', DB::raw('SUM(amount) as amountTotal'))
->groupBy('month', 'year')
->get();
Hi I have a string which is in a csv format,
Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.4.0.246,Lee.leviste,112.198.77.139:44324,Thu Dec 15 19:50:26 2016
10.4.0.202,jelozero23,112.198.78.211:32704,Thu Dec 15 19:50:59 2016
10.4.0.250,walangmaypake,112.198.78.167:22756,Thu Dec 15 19:51:00 2016
How can I remove those lines starting from ROUTING TABLE upto the last line and get this output?
Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
Thanks in advance :)
If all you need is to take a string and remove all content after "ROUTING TABLE" then this would work. However it looks like you may want to use it as a CSV after? How are you converting it to a string? Depending on how you are using it you may want to look into converting it into an array from the CSV file and then it would be done slightly different.
<?php
$string = "Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016
jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016
walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
ROUTING TABLE
Virtual Address,Common Name,Real Address,Last Ref
10.4.0.246,Lee.leviste,112.198.77.139:44324,Thu Dec 15 19:50:26 2016
10.4.0.202,jelozero23,112.198.78.211:32704,Thu Dec 15 19:50:59 2016
10.4.0.250,walangmaypake,112.198.78.167:22756,Thu Dec 15 19:51:00 2016";
$string = substr($string, 0, strpos($string, "ROUTING TABLE"));
echo "-------<br>";
echo $string;
echo "<br>-------";
// OUTPUT:
// -------
// Lee.leviste,112.198.77.139:44324,12872826,82159116,Thu Dec 15 16:30:18 2016 jelozero23,112.198.78.211:32704,157357727,2952656671,Thu Dec 15 10:59:48 2016 walangmaypake,112.198.78.167:22756,2338395760,27337738911,Thu Dec 15 14:20:12 2016
// -------
?>
Please tell how to make regex code to find all occurrence of date from the string, in the format listed below :
26 jan 2016
26th jan 2016
26 january 2016
26th january 2016
26 feb 15
26th feb 15
ie :
$string = "Test 26 jan 2016 test test test 12 Feb 15 test test test 17 January 2013 nice testing 123 6 12 2016";
I want the result as an array where i will get :
$res = array (
'2016-01-26',
'2015-02-12',
'2013-01-27'
)
Please tell how to make the same using PHP regex.
The solution using preg_match_all, array_map, date and strtotime functions (I've modified the initial string a bit to get a complex case):
$string = "Test 26th jan 2016 test test test 12 Feb 15 test test test 17 January 2013 nice testing 123 6 12 2016";
preg_match_all("/\b\d{2}(?:\w{2})? \w+? \d{2,4}\b/i", $string, $matches);
$dates = array_map(function($d) {
return date('Y-m-d', strtotime($d));
}, $matches[0]);
print_r($dates);
The output:
Array
(
[0] => 2016-01-26
[1] => 2015-02-12
[2] => 2013-01-17
)
SO I have write the code for getting the list of upcoming events in next 7 days and it is working fine and giving me the list of upcoming events.but the probmlem is that it is also giving the event record of past days also. Here is my code :
$today = strtotime("Now");
echo date('Y-m-d :H:i:s',$today)."<br>";
$leadDate = strtotime("+7 day", $today);
echo date('Y-m-d :H:i:s',$leadDate)."<br>";
$rs = mysql_query("SELECT * FROM names WHERE due_date BETWEEN CURDATE() AND '$leadDate'") or die(mysql_error());
echo "<b>Dues for upcoming 7 days</b><br>";
while($row=mysql_fetch_assoc($rs))
{
echo $row['name']." => ".$row['normal_date']."<br>";
}
I have the table :
id| name | due_date | normal_date
|1|name1|1390245240|Mon, 20 Jan 2014 19:14:00 GMT
|2|name2|1390331640|Tue, 21 Jan 2014 19:14:00 GMT
|4|name3|1390418040|Wed, 22 Jan 2014 19:14:00 GMT
|5|name4|1390590840|Fri, 24 Jan 2014 19:14:00 GMT
|6|name5|1390850040|Mon, 27 Jan 2014 19:14:00 GMT
|7|name6|1390936440|Tue, 28 Jan 2014 19:14:00 GMT
|8|name7|1396034040|Fri, 28 Mar 2014 19:14:00 GMT
|9|name8|1398708840|Mon, 28 Apr 2014 18:14:00 GMT
|10|name9|1390158840|Sunday, 19 Jan, 2014
|11|name10|1390072440|Saturday, 18 Jan, 2014
|12|name11|1390176000|20 Jan, 2015
server's today's date is 19 Jan 2014 but it is also giving me the list of 18 Jan 2014 but i am looking for the next 7 days record.and my current script is giving me the following out put:
2014-01-19 :20:44:30
2014-01-26 :20:44:30
Dues for upcoming 7 days
name1 => Mon, 20 Jan 2014 19:14:00 GMT
name2 => Tue, 21 Jan 2014 19:14:00 GMT
name3 => Wed, 22 Jan 2014 19:14:00 GMT
name4 => Fri, 24 Jan 2014 19:14:00 GMT
name9 => Sunday, 19 Jan, 2014
name10 => Saturday, 18 Jan, 2014
name11 => 20 Jan, 2015
So Why the 18 jan 2014 & 20 jan 2014 coming in record, where i am wrong.Please Help.
Thanks in Advance.
Try this code:
$rs = mysql_query("SELECT * FROM names WHERE due_date BETWEEN '$today' AND '$leadDate'") or die(mysql_error());
Instead of the CURDATE function use the $today.
i'm getting table values like below table
Name City Date
a 1 Mar 22 2013
b 2 Apr 19 2013
c 3 Apr 20 2013
d 4 Apr 22 2013
e 5 Apr 27 2013
f 6 Apr 30 2013
g 7 Jun 5 2013
i got values like above table. Todays date is Apr 25 2013. i need to group by those values from +7 and -7 like Apr 18 2013 to Jun 2 2013. Please help me to do that.
select * from yourTable
where DATEDIFF(day,'2013-04-25',[Date]) between -7 and 7
If you need the current date when query is running then use GETDATE() function instead of '2013-04-25'
Depending on the current date is created dynamically condition
SELECT *
FROM dbo.yourTable
WHERE Date >= DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())-7, 0)
AND Date < DATEADD(DAY, DATEDIFF(DAY, 0, GETDATE())+8, 0)