I want to mysqli query in laravel query. Time count - php

SELECT SUM(UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)) as TIMESTAMPDIFF FROM job_timing_logs
This is Mysqli Query
I want to this query as A laravel query
actually i am working on time count like start time, end time, pause time
my table like id ,start_time , end_time.
Thank you for help.

I am not sure how laravelish your query needs to be, but here is my take
DB::table('job_timing_logs')
->select(DB::raw("SUM(UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)) as TIMESTAMPDIFF"));

Just try this code :
$q = JobTimingLogs::sum('UNIX_TIMESTAMP(end_time) - UNIX_TIMESTAMP(start_time)');

Related

How to count remaining days in MySQL

My goal is to make a column in phpMyAdmin, which counts down the remaining days of a trial period (or something like that).
So for example when I set remainingDays to 30, I want the database to execute the query every 24 hrs
Is it possible to make something like this with only phpMyAdmin in hand, or do I have to put some code onto my website to send MySql Commands to subtract it?
Any help is greatly appreciated.
A better approach would be to have a startDate column compute the remaining days during query execution with something like:
select 30-datediff(now(),startDate) from...
Are you familiar with datediff()? Something like:
select datediff(target_date, curdate()) as days_remaining
from t;
That is, you can do this in a SQL query.

Mysql Query in Laravel active records

after a little bit search in stackoverflow I came up with this query which I wanted, and its like this:
SELECT * FROM workers where TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60)
each record is consist of these fields:
id worker_name last_activity_time per_hour
so each worker has a per_hour field that will determined as actions number per hour.
last activity is the last time worker was doing an action.
It will get records that are qualified to run at current time.
so it will determine time interval with 60/per_hour in seconds and selects the records which time passed from their last_activity_time till now is more than this calculated interval.
this works okay, But I want to know two things.
1: is this query a good approach for this problem or its slow?
2: how can I do this query in laravel 5.5 active records or query builder? and also it should return one record at a time.
thanks in advance
i think your query is fine because there were no joins and no subquerires just only condition is there. You can fire raw queries on laravel to -
$workers = DB::select('SELECT * FROM workers where TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60)');
// or you can make use of query builder as follows
$workers = DB::table('workers')->whereRaw('TIMESTAMPDIFF(SECOND, last_activity_time, NOW()) >= (60/per_hour*60')->first();
this should work
\DB::table('workers')->whereRaw(...)->first();
https://laravel.com/docs/5.6/queries
1: is this query a good approach for this problem or its slow?
It will depend on how many workers you have, you should try and see

How to optimize this mySQL query in terms of performance?

How can I optimize the follow query ?
select
hash,page,timestamp, count(*) as total
from behaviour
group by hash, page
having total > 2 AND timestamp >= Now() - interval 5 minute
Thank you
This query it's not wrong.
Maybe with a bigger volume, you could have a problem with performance.
Try use "WHERE CONDITIONS" always. (In your context, i don't know if is possible).
Another tip, is EXPLAIN on your query!
Use "explain" before your query. (explain select hash, timestamp, count(*)........).
In this way, you can discover how many results your query is returned.

Get value between to hours from mysql using php

I'm trying to get a specific show description from my DB but I really don't know how, I know i'm new into this, the table (guide) have 2 DATETIME values "start" and "end"
If I use this:
$sql = mysql_query("SELECT * FROM guide WHERE start >= CURDATE()");
Only return the first value from the table, and inclusive its the wrong value, please I need some help to get this, because I don't find a solution from other on this web
You should be able to use mysql's between function to pull the records in the current time range.
select * from guide where now() between start and end
To limit the returns you can add in additional parameters, this may give you back no results though so have a default value.
select * from guide where channel = $channel_ID and now() between start and end
You also should look into parameterized queries and updating your driver. Having variables in your query isn't the best practice.

Get value of NOW() in MySQLi PHP

Assume that your webserver is detached from your database server. Their clocks might not be properly synchronized (down to milliseconds etc.).
You perform an insert such as "INSERT INTO sometable (VALUE,VALUE2,DATETIME) VALUES ("something","something else",NOW());"
Is there any way to get the timestamp generated by MySQL back into PHP? In the same fashion as when using $sqlObject->insert_id to get the AUTO_INCREMENT value of the last inserted row.
Edit:
I realize I could just use the insert_id and run a SELECT to get the timestamp, but this would require that I run another statement. I am wondering if it is possible in a single statement.
You can not get the direct value but you can do it via 2 ways.
1) Make a select query like select now() as cur_time. and take its value and use in the 2nd query. [ Its possible that you might get different time for this solution, SO i will suggest not to go with this solution and better you go with option 2. ]
2) After inserting the data get its ID and and make a query and get the inserted date time.
If your PHP and MYSQL server date time are same then you may use php's date function to get the current date / time .

Categories