Simple DateTime sql query with php object - php

This is my Query . It does not give result.If i place a quote the query runs. How to place that quote.
Select sum(r.views) as sum_views ,r.revenue_date ,
l.lot_id ,l.lot_name,r.type_name
FROM tbl_revenue_upload r , tbl_lot_details as l
WHERE AND l.lot_id ='.$search['lot'].'
And revenue_date >= '.$search['from_date'].'
And revenue_date <= '.$search['to_date'].'
group by r.type_id ,month(revenue_date)

'Select sum(r.revenue) as sum_revenue ,sum(r.views) as sum_views ,r.revenue_date ,l.lot_id ,l.lot_name,r.type_name
FROM tbl_revenue_upload r , tbl_lot_details as l
WHERE r.video_id = l.video_id AND l.lot_id ='.$search['lot'].'
And revenue_date >= "'.$search['from_date'].'"
And revenue_date <= "'.$search['to_date'].'"
group by r.type_id ,month(revenue_date)

Related

Can not get the required value using sql query in PHP and MySQL

I had one issue with my sql query. I am unable to get the proper data using PHP and Mysql. I am explaining my query below:
$todayDayId = date('N');
if($todayDayId > (int)$day_id) {
$diff = (7-$todayDayId) +(int)$day_id;
}
else {
$diff = (int)$day_id - $todayDayId;
}
$wantedDate = date('Y-m-d', strtotime('+'.$diff.' day'));
$sql="
select
c.cat_id,c.cat_name,c.special,s.subcat_id,s.subcat_name,s.status,s.order_no as order1,c.order_no as ord2, s.cat_id as scatid,b.status,sl.day_id,sl.date_from,sl.date_to
from db_category as c
inner join db_subcategory as s on s.cat_id=c.cat_id
inner join db_restaurant_detail as r on s.subcat_id= r.subcat_id
inner join db_restaurant_basic as b on r.member_id=b.member_id inner join db_special_images as sl on r.day_id=sl.day_id
where
r.day_id='3'
and c.special='2'
and r.city='1'
and (
((sl.date_from IS NULL or sl.date_from='') and (sl.date_to IS NULL or sl.date_to=''))
or (sl.date_from <='2016-12-07' and sl.date_to >= '2016-12-07' )
)
and b.status=1
and s.status=1
group by s.subcat_id
order by cast(ord2 as unsigned), scatid, cast(order1 as unsigned) asc"
$sqlqry = mysqli_query($connect,$sql);
while($row=mysqli_fetch_array($sqlqry)) {
$data[]=array("subcat_id"=>$row['subcat_id'],"subcat_name"=>$row['subcat_name']);
}
The above giving the below output.
[{"subcat_id":"4","subcat_name":"WHISKY"},
{"subcat_id":"60","subcat_name":"SCOTCH"},
{"subcat_id":"63","subcat_name":"RUM"},
{"subcat_id":"56","subcat_name":"BEER"}]
But here my problem is for {"subcat_id":"63","subcat_name":"RUM"} sl.date_from=2016-11-23 and sl.date_to=2016-11-30 but for wantedDate=2016-12-07 the RUM is coming which should not come. Here I need if sl.date_from='' and sl.date_to='' then it can come but if any date is present then it will check. The wanted date should lie in between the given database date.
Please help me.
Thanks in advance.

Too heavy query execution

I have a Database with 187840 lines .
When i execute this query i have this message Query execution was interrupted
TOO HEAVY QUERY
SELECT days.day,count(U.sig_name) as number
FROM days
LEFT JOIN linked U ON
days.day = date(timestamp)
AND
U.sig_name REGEXP "^Tester"
GROUP BY days.day;
What is th solution ?
This is your query:
select days.day, count(U.sig_name) as number
from days left join
linked U
on days.day = date(timestamp) AND U.sig_name REGEXP "^Tester"
group by days.day;
You have a problem because of the function call around timestamp. You might find this version better:
select days.day,
(select count(*)
from linked u
where u.timestamp >= days.day an du.timestamp < date_add(days.day, interval 1 day) and
u.sig_name not like '%Tester%'
)
from days;
For performance, you want a composite index on linked(timestamp, sig_name). This eliminates the outer aggregation (the aggregation uses the index instead), and allows an index to be used for the matching.
You can handle massive data using LIMIT:
$limit_size = 10000;
$flag_done = false;
for ($i = 1; ! $flag_done; $i++) {
$queryString = "SELECT days.day,count(U.sig_name) as number from days left join linked U on days.day = date(timestamp) AND U.sig_name REGEXP "^Tester" group by days.day LIMIT $index*$limit_size, $limit_size";
if($result = mysql_query($queryString, $db)){
[WHAT YOU WANT TO DO WITH RESULT HERE]
} else $flag_done = true;
}

How would I add a date range query in my current MySQL Query?

I have a question how do I add a query for a date range here in my MySQL query. The date is generated only.
Here is my Query:
SELECT TS.id AS TSid, employeeid, `date` AS date_d, TS.TI1 AS TI1, TS.TO1 AS TO1,
CONCAT(E.lastName,', ',E.firstName,' ',E.middleInitial,'.') AS 'fullname',
(TIME_TO_SEC(CAST(TS.`TI1` AS time))/3600) AS 'timeinint',
(TIME_TO_SEC(CAST(TS.`TO1` AS time))/3600) AS 'timeoutint',
E.startDate, C.companyName, B.branchName,
R.mon, R.tue, R.wed, R.thu, R.fri, R.sat, R.sun
FROM timeschedule AS TS
LEFT JOIN employee AS E ON TS.employeeid = E.id
LEFT JOIN company AS C ON E.companyid = C.id
LEFT JOIN branch AS B ON E.branchid = B.id
LEFT JOIN restday AS R ON R.id = E.restDayid
WHERE TS.`show` = '1'
ORDER BY employeeid ASC
HEre is how I get the input:
$mfrom = $this->input->get('mfroma'); //From: Month
$yfrom = $this->input->get('yfroma'); //From: Year
$yto = $this->input->get('ytoa'); //To : Year
With the fields you've specified (from month and year and to year) you can add these conditions into your WHERE clause. I'm going to use the E.startDate field for this example:
$sql = "-- your query here...
WHERE
TS.`show` = '1'
AND MONTH(E.startDate) >= " . $mfrom . "
AND YEAR(E.startDate) BETWEEN " . $yfrom . " AND " . $yto . "
ORDER BY employeeid ASC";
Docs:
- MONTH()
- YEAR()
Note: PHP parses variables inside double quotes, but I personally prefer to keep PHP variables outside of them.

MySQL data - Count number of phone calls at the same time

I have a MySQL table with phone calls. Every row means one phone call.
Columns are:
start_time
start_date
duration
I need to get a maximum phone calls called at the same time. It's because of telephone exchange dimensioning.
My solution is to create two timestamp columns timestamp_start and timestamp_end. Then I run a loop second by second, day by day and ask MySQL something like:
SELECT Count(*) FROM tbl WHERE start_date IN (thisday, secondday) AND "this_second_checking" BETWEEN timestamp_start AND timestamp_end;
It's quite slow.
Is there a better solution? Thank you!
EDIT - I use this solution and it gives me proper results. There is used SQL layer dibi - http://dibiphp.com/cs/quick-start .
$starts = dibi::query("SELECT ts_start, ts_end FROM " . $tblname . " GROUP BY ts_start");
if(count($starts) > 0):
foreach ($starts as $row) {
if(isset($result)) unset($result);
$result = dibi::query('SELECT Count(*) FROM ' . $tblname . ' WHERE "'.$row->ts_start.'" BETWEEN ts_start AND ts_end');
$num = $result->fetchSingle();
if($total_max < $num):
$total_max = $num;
endif;
}
endif;
echo "Total MAX: " . $total_max;
Instead of running it second by second, you should for each row (phonecall) see what other phone calls were active at that time. After that you group all of the results by the row's ID, and check which has the maximum count. So basically something like this:
SELECT MAX(calls.count)
FROM (
SELECT a.id, COUNT(*) AS count
FROM tbl AS a
INNER JOIN tbl AS b ON (
(b.timestamp_start BETWEEN a.timestamp_start AND a.timestamp_end)
OR
(b.timestamp_end BETWEEN a.timestamp_start AND a.timestamp_end)
)
GROUP BY a.id
) AS calls
Creating an index on the timestamp columns will help as well.
I'm going to add something to #reko_t answer. I think there is a use case to consider.
Calls that start before and ended after - Calls completely overlapped
So, how about:
SELECT MAX(calls.count)
FROM (
SELECT a.id, COUNT(*) AS count
FROM tbl AS a
INNER JOIN tbl AS b ON (
(b.timestamp_start BETWEEN a.timestamp_start AND a.timestamp_end)
OR
(b.timestamp_end BETWEEN a.timestamp_start AND a.timestamp_end)
OR
(b.timestamp_start <= a.timestamp_start AND b.timestamp_end >= a.timestamp_end)
)
GROUP BY a.id
) AS calls
How about:
SELECT MAX(callCount) FROM (SELECT COUNT(duration) AS callCount, CONCAT(start_date,start_time) AS callTime FROM tbl GROUP BY callTime)
That would give you the max number of calls in a single "time". Assuming start_date and start_time are strings. If they're integer times, you could probably optimise it somewhat.

how to convert propel criteria in symfony

select a.id, b.title, b.start_time, b.end_time from tv_channel a
left join tv_program b on a.id = b.tv_channel_id and b.start_time >= ‘2011-09-23 12:00:00′ and b.end_time <= '2011-09-23 14:30:00'
order by a.code
limit 0, 10;
–pager object
tnx
What's the question ? Do you want to write this SQL query in Propel ?
<?php
TvChannelQuery::create('a')
->joinTvProgram('b')
->addJoinCondition('b', 'b.StartTime >= 2011-09-23 12:00:00')
->addJoinCondition('b', 'b.EndTime >= 2011-09-23 14:30:00')
->orderByCode()
->limit(10)
;
Something like that should work but be be careful about values passed in addJoinCondition, there is no binding here and if you want to change these values you have to use it to prevent SQL injections or other security issues.
$c = new Criteria();
$c->addLeftJoin(tv_channel.id = tv_program .channel_id);
$c->add(tv_program.start_time, '2011-09-23 12:00:00', Criteria::GREATER_EQUAL);
$c->add(tv_program.end_time, '2011-09-23 14:30:00', Criteria::LESS_EQUAL);
$c->addAscendingOrderByColumn(tv_channel.code);
$c->setLimit(10);
$rs = DoSelect($c); //actual select execution here...

Categories