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.
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 need to select records based on the month and year, The month and the year will come from PHP
so I have the query like
SELECT id, created FROM table_name WHERE YEAR(created) = 2020 AND MONTH(created) = 05 LIMIT 500;
Here created is the datetime field, The problem with this query is, It is working fine if the table has less records but when table records are increasing the query is becoming very slow
I have more than 10,000 records in my table for 2020-05 and a total of 1 million records so it takes approx 11 seconds to execute this query, I suspect this is because the query is doing a full table scan
Help me with a solution to make this query execute faster?
You are using YEAR() and MONTH() function in where clause when using a function in where clause MySQL doesn't use the index of that column to executing your query.
You can change your query to below and make a try
SELECT id, created FROM table_name WHERE created between '2020-05-01 00:00:00' and '2020-05-31 23:59:59'
** Don't forget to add an index to your field.
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.
I have a PHP scirpt that is always querying all the data from a database table and it's getting pretty slow. I really just need the data of a specific month and year.
Is there a simple way to get only those entries? For example, everything from February 2013?
The column that stores the dates in my table is of type datetime, if that applies to the solution.
You can add that condition in the WHERE clause of your select statement. I would recommend using BETWEEN operand for two dates:
SELECT myColumns
FROM myTable
WHERE dateColumn BETWEEN '2013-02-01' AND '2013-02-28';
If you mean to say you want everything beginning with February 2013, you can do so using the greater than or equal to operator:
SELECT myColumns
FROM myTable
WHERE dateColumn >= '2013-02-01';
EDIT
While the above are my preferred methods, I would like to add for completeness that MySQL also offers functions for grabbing specific parts of a date. If you wanted to create a paramaterized query where you could pass in the month and year as integers (instead of a start and end date) you could adjust your query like this:
SELECT myColumns
FROM myTable
WHERE MONTH(dateColumn) = 2 AND YEAR(dateColumn) = 2013;
Here is a whole bunch of helpful date and time functions.
You should index the datetime field for added efficiency and then use Between syntax in your sql. This will allow the mysql engine to remove all records that you are not interested in from the returned data set.
I have this little script that shows one wisdom each day.
so I have three columns.
Id wisdom timestamp
1 wisdon 1 4/1/2012
2 wisdon 2 4/1/2012
3 wisdon 3 4/2/2012
and I want to fetch array of one wisdom for each day
I looked around your website, but unfortunately I didn't find something similar to what I want.
also I got this code
$sql = mysql_query("SELECT DISTINCT id FROM day_table group by timestamp");
but this also not working.
any ideas?
is it possible to make a counter of 24 hours update wisdom date?
please give me some help.
You can make another table that is called wisdom_of_day
The table would have the following columns, id, wisdom_id, date
Basically each day you can randomly select a wisdom from your wisdom table and insert it into the wisdom day table. You can also add a constraint to your date column so it is distinct. It is important that it is a date column and not a timestamp since you don't care about time.
Then you can retrieve the wisdom of the day by querying based on the date.
It's possible I read your question wrong and you just want to select one wisdom for each day, but you want to show multiple days and you want to get the data from your table.
If so, the reason your query is not working is because you are grouping by a timestamp which includes the date and time. You need to group it by date for it to group like you want.
Here is a query that will group by the day correctly. This will only work if you have a timestamp field and are not storing a unix timstamp on an int column.
select id, wisdom, date(timestamp) date_only from day_table group by date_only order by date_only asc;
Hmm, I noticed that your timestamp values are in some kind of date format, maybe as a string? If so the above query probably won't work.
First compute number of days since 1970
SELECT DATEDIFF(CURDATE(), '1970-01-01')
Then insert this number inside RAND, for example:
SELECT * FROM table ORDER BY RAND(15767) LIMIT 1;
Rand with number as argument is deterministic.
Full query:
SELECT * FROM table ORDER BY RAND((SELECT DATEDIFF(CURDATE(), '1970-01-01'))) LIMIT 1;