Simple Update Function MySQL - mysql

I have a CMS coded in ColdFusion 9 with basic login functions to work with a socket server that I'm working with. Recently I setup a "VIP" subscription w/payment, but then I realized, I have no SQL to remove the subscription after 2 months have passed. Here's my basic request; I need some SQL to execute automatically if the timestamp value of date_purchased is 2 months old. It needs to delete the row in vip_purchases for the session user (Only the user), and update the column vip to 0 in users.
I need this to execute for the session username ONLY, and only if the timestamp is 2 weeks old, and ONLY if the value in VIP is 1.
I was thinking along the lines of:
update users set vip = 0 where username = session.username if vip = 1 if date_purchased = 2 months old
I realize this isn't SQL, that was just to attempt to make it a little more clear.

Close. :)
UPDATE users
SET vip = 0
WHERE username = sessionname
AND vip = 1
AND date_purchased <= (today - number of days wanted)
You'll need to replace the last part of the WHERE (the today - part) with whatever timeframe condition you want; your post is confusing, as it mentions 2 weeks old and 2 months old in the same context.

If you plan to run this reoccurring task via Coldfusion (as opposed to internal to the MySQL database), you should setup a Scheduled Task via the Coldfusion Administrator. The task could launch a script once daily to clean up all of the old accounts.
Once that is decided, you can create a .cfm or .cfc file that includes the task.
This can be a simple file that includes a cfquery or a cfstoredproc depending on where you want your logic to run. Cfquery will run the logic on the web server in Coldfusion, the cfstoredproc will ask the database server to run MySQL logic on the database server. It would likely be best to run it as a stored procedure.
In this case, you would not need to even check the username. Just SELECT then loop over and UPDATE all users where vip = 1 and the date_purchased has expired.

Related

How to update large database in mysql database using cronJobs

I am very thank full if anyone help me to resolve this issue.
Detail: I have a mysql table where 110000+ record stored with Unique application(column) number.
Now there is Detail API which will take only 1 application number at a time in URI parameter and return details of that application number it will take Approx 1 min to respond for now.
Now i need to update that records(multiple column) using Cron Job Scheduling to always updated record in database.
and it will progressively update.
Flow: Get application number from database -> Call detail API -> update there record on database.
Issue: there is large number of record in database so we can not call API for all application number in ones.
I am using Laravel 7 and Guzzle HTTP client 7.2 HTTP Client for API calling.
Any suggestion are welcome !
Update: (Tested) I am thinking to do something like this and agree with #bhucho comment to call cron in every 15 minute
We create one more column in table for last_updated_id default 1
and we will write a query to fetch application number something like and get 100 or 500 records in one slab using Laravel take methods from here Database Queries
$latestIdInColumn = myTableClassRef::max(last_updated_id);
$applications = myTableClassRef::select('application_number)->where('id', '>', $latestIdInColumn)
->take(100)
->get()->toArray();
Here we call detail API and update for each application number, when there is last application update done we will store that id in last_updated_id.
When again cron call we have that last_updated_id so we will apply same filter in next query where id '>' $latestIdInColumn. now we will get next 100/500 records where id is greater then $latestIdInColumn.
if ( $applications )
{
For all other available id
} else {
when record not found.
Here we will reset the last_updated_id = 1
}
Now function will fetch from id 1.
It is not tested yet i just planing to do this, i am happy to have
feedback on this.
Note: Now API not taking 1Min to respond I resolved this issue.
Update 2: It is working good.

How can I run a query in a specific time using MySQL or PHP?

is it possible to run a query in a specific time like for example every seconds/minutes. By the way I am building a simple auction system and the query I am talking about is to check if there are products that are already expired. Means their datetime_end is less than the current date and time.
So I have a query like this:
SELECT id, datetime_end FROM auction_product WHERE datetime_end < NOW() AND `status` = 0;
And after identifying if there are products that are already expired I will create a query that will change their status to 1 means the status is cancelled/closed.
Is there a function in MySQL that can run a certain query? Like automatic query. Or should I make an Ajax that will run every seconds and check if there is an expired product? Can you give me some idea about this?
Because in my current setup I just put the AJAX in product page and homepage. Means if the user is in the other page my AJAX will not run. So I think I will put my created AJAX somewhere in the header or footer or in a global JS. If I did that does it affect the performance of my page load? Because it will run every seconds.
That's all thanks. I hope you can give me some idea about this. :)
You could do this in MySQL using scheduled events e.g.:
CREATE EVENT IF NOT EXISTS expire_products
ON SCHEDULE
EVERY 1 MINUTE
STARTS CURRENT_TIMESTAMP
DO
<insert your update statement here>;
You need to enable the event scheduler first either via the command line argument:
--event-scheduler=ON
or the my.cnf/my.ini option:
event_scheduler=ON
or by running the following in MySQL:
SET GLOBAL event_scheduler = ON;
See https://dev.mysql.com/doc/refman/5.7/en/create-event.html for more details.
You don't need to check it every second on the client side. In case someone visits the page of the product, you'll run an ajax there to check if there are enough products left or not. If there are no products left you can update the database on the page itself.
Now you also want to make sure it is regularly updated, so you can run a script on the server side on a Cron Job. But, you also need to make sure you don't run some heavy resource intensive scripts on it. You can run a cron job about every hour or two hours to regularly update it from the server side. And in case any of the users views a product, you will update it automatically with the ajax, so the next time a user visits, in between two cron jobs, they will see the page being already updated because of the earlier user. This will keep the pressure out of your server by distributing the work.
So the idea is somewhat like this:
1)user enters-> visits page-> runs ajax to check if products are left -> update db if products are over
2)cron job checks if products are left every two hours-> updates db if products are over

update mysql table records automatically every first of year using laravel 4.2

I have users table have column named balance I want to auto update balance column for all users First every year + value (30) as every user will gain 30 points credit yearly on first of year how can i make this query and make it auto run First every year
sample for my table
id name balance
1 name1 20
2 name2 31
2 name3 14
Honestly, I would leave laravel completely out of the picture. Use either mysql's built-in event scheduler or use your OS' similar functionality (e.g. cron or windows task scheduler) to schedule the run of bit of sql script. If you use OS event scheduler, then use mysql's command line client to execute the sql script.
Depending on your business requirements, probably you want to run the script every day to update those balances that were registered 1 year agao.
Your sql will be sg like the below one:
update table set balance=balance+30 where field_for_creation_date=curdate() - interval 1 year
I assume that you store the date of registration / activation from which the 1 year is calculated in a field, I named it field_for_creation_date in the above query.
I solved this problem by using c-panel Cron Jobs I created php script for connecting to database and update the records then i put this script path in Cron Jobs command like this
php -f /script-path/scriptName.php

SSRS - How can I conditionally send a report to a mail address

Let me specify the environment. The client has UI for entering orders in SQL Server database. A Windows Service one another machine is processing these orders from database at particular intervals. Sometimes the Windows service is stopped then orders piles up. To avoid that I have created a SQL Server Report which is run at an interval of 5mins. It checks how many orders are processed and creates a status report. What I want, if count of processed order is zero then the report be mailed to system administrator. Then he will check the machine where the service is hosted and restart the service, so that all rework is avoided. So the question is how to implement this conditional delivery of report. In short, if count is zero no report be mailed otherwise it must.
This is 4 years late, but for anyone who stumbles onto this, a Data Driven Subscription will do the trick.
In short, you can create a query that returns the recipients if there is data, or NULL if there isn't any data.
This can be done by using Data Driven Subscription, where we have to put some SQL condition query in which it return ToEmail Address if data is present then ToEmail Address will not be blank or NULL
Below are some code that will help
IF EXISTS(SELECT 1 from Table where datefield = CONVERT(varchar(13),GETDATE() -1 ,23)+' 00:00:00.000' )
Select ToEmailAddress ='abc.pqr#xyz.com'
ELSE
Select ToEmailAddress =''
It's a common problem with SSRS 2005 (not sure about 2008) - there's no easy way to stop an empty report from being sent.
Link
The solution I had to use (and yes, I know it's ugly, inefficient, and somewhat embarrassing...), is to run your search query in a stored procedure. If rows are not found, cause an error. If rows are found, re-run your query.
SELECT * FROM TABLE
IF ##RowCount = 0
BEGIN
SELECT 1/0
ELSE
SELECT * FROM TABLE
END
Returning an error will prevent the subscription from emailing the report.

Automatically print out orders

I am wanting to create a VB.Net app that checks a database and then prints out the latest orders, the database is mysql and i need it to periodatically check and only print new orders i.e. not print ones it has already printed out.
Any advice would be greatly appriciated.
Kyle
My suggestion is to add a printed flag to your database, i.e. add a printed field in the database which stores 0 by default denoting the entry has not been printed and a 1 that denotes the entry has been printed.
I would then create an application which checks the database table for all entries where flag = 0.
These entries are the ones that need printed.
Print each entry an set its flag to 1 in the database. Setting the flag to 1 ensures that the next time you poll the database the entries will be ignored.
I'd recommend breaking the problem down into pieces:
"checks a database" - you'll need to create a connection to a relational database. You'll have to set up either an ADO.NET or ODBC connection to MySQL.
You don't say if the database is running on the same machine as the app. If it's not, you'll have to be able to connect to it on the network.
Make sure that you GRANT the appropriate permissions in MySQL. Don't just use the admin credentials to log in. Set up a separate ID for your app.
"then prints out" - your app will have to find a way to acquire information about a printer. You don't say if it's connected directly to the server on which the job will run or if it's a networked printer. Be sure your app can see the networked printer if it's the latter.
"the latest orders, the database is mysql" - try writing a separate query to test getting only the latest orders.
Does your schema include a way to mark which orders have been printed and when? Your app should INSERT a record noting the timestamp when each order is printed. You can JOIN on this table to figure out what the latest, unprinted orders are.
"and i need it to periodically" - your app needs to be a cron job that's run on a schedule that you specify. Every weekday, hourly? Once a night? You decide when you set up the cron job.
You could try using LINQ and dbLINQ for connecting with MySQL. If you are using .NET Framework 3.5 you may as well take advantage of something like LINQ to speed up development time.
If using WPF for the client application you could use a FlowDocument to layout the order details and then handle printing of the orders. Once they are printed you can update the database or store details locally to mark that order as printed. It depends on your database schema.