I have LAMP server hosting a local website for my firm where I need to enter data each month.
The data is values from electricity meters, which are measuring energy consumption in every individual office. I need to insert a unique set of data each month, no duplicates in the same month. How can I do that?
I would create a computed column on YYYY-mm part of the date and add a unique index on it, e.g.:
ALTER TABLE <your_table> ADD yyyymm as DATE_FORMAT(date, '%Y-%m');
CREATE UNIQUE INDEX idx_yourtable_office_id ON your_table(office_id, yyyymm);
If your data is sorted in database you have to select the last item and compare it with you inputted data if it's not equal insert it with query. Else show a message that the date is wrong.
Related
I want to do a query with thousands of tuples. I need to save the first ID, last ID and date saved in a historic table by day in a new table. I have data from 2020 to 2022. Every day could 600.000 rows or more. I have thought two solutions:
Doing a query every time with limit 600.000 and save the first id, last id and date, all of this order by dates or ids.
Doing a query day by day and get the first and the last id.
The problems are that these querys could delay so much because i am doing orderings.
I´m doing this with SQL and need execute this in PHP with a cron every day to save the data of the day. First, i´m building the new table with the data of past.
Someone would know one tip or antoher form to do this.
THANKS!
You can do this (result here)
select date, min(id) as min, max(id) as max
from logs
group by date
I'd like to add a column to my database which displays the amount of time in days since a user signed up.
Currently I have a field which displays the date they signed up in unix.
Is it possible for the new column to increase its fields by 1 each day?
You can basic SQL to get this information for you dynamically. DATEDIFF() will be what you need:
SELECT
DATEDIFF(CURRENT_DATE, FROM_UNIXTIME(date_signed_up)) AS days_since_signup
FROM
tablename
current time - timestamp_of_registration:
https://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html#function_datediff
SELECT DATEDIFF(CURTIME(), SIGNUP_DATE);
I'm new to MySQL and PHP but was wondering if someone could help me with a little project I'm doing for my boss.
I have a SQL database (MyDB) and a table in there (mytable) with two columns - the first column (index) is an auto-incrementing integer from 1-10, the second column (date) has different dates and timestamps in the format of Year-month-day time 2013-04-12 1326
I'm trying to create a simple PHP page that first gets the current date (easy enough) then looks at the table and shows the number of rows that fall within yesterday's date. For example, if I have 3 rows with 2013-04-11 XXXX and 2 rows with 2013-04-12 XXXX (and today is the 12th April 2013) the page will display 3. (The time is not important but we can't remove it from the table as it's auto created by one of the other staff's programs and he refuses to change it).
So far I've got my php page, done a connection to the DB and defined two variables:
$startdate = date('Y'."-".'n'."-".'d'." "."0000");
$enddate = date('Y'."-".'n'."-".'d'." "."2359");
As the timestamp doesn't matter I've gone for the min/max possible on the variables. I realise this will only give the current date, trying to work out how to get it to display the previous day as the date in the variable.
Now I'm trying to create a sql query that will count the number of rows where the date field falls within the startdate and enddate variables (-1 day) but not too sure where to start or how this would look. I then need to output this as a variable in PHP so I can echo it later in the page.
Anyone able to point me in the right direction? Hope any of this makes sense.
You could write a query with no params to do this (if its always just yesterday).
SELECT * FROM <table>
WHERE DATE_FORMAT(<date column>,'%j-%Y') = DATE_FORMAT(DATE_SUB(now(),INTERVAL 1 DAY), '%j-%Y');
Date functions in the where clause might not be super awesome performance wise
I have two field in a database table departureDateTime and arrivalDateTime and the values like
departureDateTime=03/18/2012 1:05 PM
arrivalDateTime=03/18/2012 3:15 PM
I have hundreds of records in the table.
I need to sort and display according to the duration from these two time. I know to calculate the duration from two dates
Duration=(strtotime($arrivalDateTime') - strtotime($departureDateTime))/3600
But how I write a mysql query to sort and display these 100 records from database
Does any one any idea?
Thanks
Change your departureDateTime to DATETIME instead of varchar or whatever you are currently using. Then you would use something like this:
SELECT other, stuff, TIMEDIFF(departureDateTime, arrivalDateTime) as theDifference FROM myTable ORDER BY theDifference ASC LIMIT 0, 100
If you kept your date and times in a date time column type then you wouldn't have this problem. Consider changing.
There are mysql functions to calculate dates and time too
http://dev.mysql.com/doc/refman/5.6/en/datetime.html
If not, you can do sorting in PHP using usort.
Using MySQL and PHP; I'm trying to build an index that contains the averages from table_1 grouped by:
type, name, hour, day, month, year
So I need to know which combination's of values are in table_1 so I know what to put in my AVG() queries.
What I want is to figure out all the different combination's that can be made when comparing the following rows in the table:
type
name
hour
day
month
year
Here's an example of table_1:
ID|type|name|location|amount|year|month|day_num|day|hour|minute|second
1|car|ben|1|1.00|2010|10|01|Friday|03|05|45
1|car|bob|1|3.00|2010|10|01|Friday|04|05|45
2|cow|bob|2|2.00|2009|07|12|Sunday|09|10|12
2|cow|ben|2|4.00|2009|07|12|Sunday|10|10|12
So what I would end up with is:
type|name|year|month|day|hour
car|ben|2010|10|01|Friday|03
car|bob|2010|10|01|Friday|04
cow|bob|2009|07|12|Sunday|09
cow|ben|2009|07|12|Sunday|10
How would I format a query to do that?
Since you just want the combinations that exist, you can simply run this query:
SELECT DISTINCT type, name, hour, day, month, year FROM table
This goes through all of the rows, and for each combination that exists in the table, that combination will be output once in the result set.