Insert records based on date wise in table. like
'test1','test2','2017-12-10'
'test11','test22',2017-12-08'
'test12', 'test23', '2017-11-10'
latest date records will be on top. please suggest me how to do it.
Related
I'm inserting data into a mysql table via a php script, however I wanted to know if there is a better way to insert data if the previous row is not identical.
For example:
Stock Price Bid Ask Timestamp
AAPL 232.23 232.22 232.23 1879239289
TSLA 500.23 500.12 500.26 1879239346
If the next record for aapl is identical to the most recent record of aapl. I don't want to insert it.
Likewise with TSLA.
I know I can do a query before insert, sort by id desc, and check each column - but is there another way using mysql trigger or condition?
Make the ID as primary key in MySQL and into the php code create the query between try catch.
https://stackify.com/php-try-catch-php-exception-tutorial/
Hopefully this one isn't too bad I just haven't been able to figure it out.
So I have a table with data by date. I need to aggregate the data in that table off of a field, lets say profit and customer, and then insert that into a table that is named for the month the transaction took place.
For example lets say I have the follow data:
20170101, Michael, 100$
20170301 George, 200$
20170104, Carla, 50$
20170115, Michael, 500$
I would need to insert into a table called January the aggregate for all the transactions that happened in january by customer. That would look like this:
January
Michael, 600$
Carly, 50$
March
George, 200$
Hopefully I explained that well enough for it to make sense.
Thank you in advance for all the help!
edit1:
I think I explained it badly. The group by and aggregate of the data is not my problem. It is the inserting it into another table based on the month. I do not know which table it will go into until i hit the record. So basically I need to dynamically insert into a table based on a column in a select statement, the date in this case
You don't need aggregation in this case. You just need to fetch the data for each month and insert into the appropriate month table. This looks much like a task that can be done using a stored procedure. Since you want to insert the data into a table (the table of the month) you have to see that the data in this table does not get polluted by multiple insertions. To prevent that it will be better to go by a trigger on your tables on insert or update.
See MySQL Triggers
Why not just aggregate them in a group by at the end? If you have the data already in one table, why duplicate it? Do something like:
GROUP BY SUBSTR(date_column, 4, 2)
or
WHERE SUBSTR(date_column, 4,2)=01
mysql query to find data entry not present in table for particular date
I wish to find particular fridays date whose entru is not present in the table
You can query the things that exist in the data. So to find missing dates, you can create a table with all dates in your desired date range and make a query which selects those dates that do not exists in your other table.
I am trying to create a table in mysql. I want to store future dates in that table. I am trying following query:
SQL:
create table dates(submission _date date check(submission _date>curdate));
But this check Clause is not working properly. It accept all dates. Please give me solution.
I'm wondering if it is possible to find all new rows in a table that were added in the last week if the table has no date column to signify when a given row was inserted into the table?
If so, can someone please advise me on how to accomplish this?
Or do I require a date column?
Thanks
You do need some kind of timestamp, Or keep external records somewhere, what was added last week ;-). Do you have some sort of auto-incrementing column id?
You do need a date col. 1 week means u are specifying time duration which requires dates or time field.
Otherwise you can keep a Status Column whose values are active or inactive. Each time row is displayed to user, you can update the status to inactive
You definitely need a date column. You could add one today, allowing all previous entries in the table to be null for that field, and have it start populating from now. After a week, your problem is solved.
Your best option is a timestamp, then you can select any particular period.
Alternatives would be an autoincrement field and you record the last number of the previous wee.
Or you have a status field that defaults to "NEW" and then you set it to "OLD" or whatever once you have processed it.
auto-incrementing id can be helpful.save a record id(last inserted record id).After a week compare id's with that specific record id will result you the newer rows as all the rows with larger id's will be of last week