Post by imager » Fri Oct 08, 2021 11:00 pm

I need some assistance with a SQL query and update that I would like to do.

In my MySQL Database, I would like to update the date_added field to the current date for ALL records.

Any SQL experts that could suggest the statement(s) to use to perform this?

Thanks..

New member

Posts

Joined
Fri Nov 09, 2012 7:05 pm

Post by straightlight » Fri Oct 08, 2021 11:12 pm


Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by imager » Fri Oct 08, 2021 11:48 pm

I will have a look at the notes you mentioned.

Running OC 3.0.2.0 and was looking at running the SQL query through cPanel and not through OpenCard at all.

New member

Posts

Joined
Fri Nov 09, 2012 7:05 pm

Post by by mona » Sat Oct 09, 2021 12:02 am

Then I believe this is the link you are searching for
https://stackoverflow.com/

DISCLAIMER:
You should not modify core files .. if you would like to donate a cup of coffee I will write it in a modification for you.


https://www.youtube.com/watch?v=zXIxDoCRc84


User avatar
Expert Member

Posts

Joined
Mon Jun 10, 2019 9:31 am

Post by thekrotek » Sat Oct 09, 2021 2:00 pm

UPDATE oc_product SET date_added = NOW()

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by straightlight » Sat Oct 09, 2021 7:16 pm

From the master branch version, by pulling the db_schema() function and the helper file from the system/helper/db_schema.php file, you could use something like:

Code: Select all

require_once(DIR_SYSTEM . 'helper/db_schema.php');

$tables = db_schema();

foreach ($tables as $table) {
	$table_query = $this->db->query("SELECT * FROM information_schema.COLUMNS WHERE TABLE_SCHEMA = '" . DB_DATABASE . "' AND TABLE_NAME = '" . DB_PREFIX . $table['name'] . "'");

	if ($table_query->num_rows) {
		for ($i = 0; $i < count($table['field']); $i++) {
			if (($table['field'][$i]['type'] == 'date' || $table['field'][$i]['type'] == 'datetime') && $table['field'][$i]['name'] == 'date_added') {
				$this->db->query("UPDATE `" . $table['name'] . "` SET `" . $table['field'][$i]['name'] . "` = NOW()");
			}
		}
	}
}
If you have enough amount of resource on your server to increase the post size in your php.ini or .user.ini file, you could run this query to update all date fields in shot after doing a full backup of your store - including database. Alternatively, at best, to run this query on a development / virtual server.

However, take note that applying these changes won't change anything based on the fact that your payment and shipping service providers may not provide this option to reset your dates. Which means, the data between the two databases, local and providers, will provide inaccurate results with the datetime period.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by thekrotek » Sat Oct 09, 2021 7:58 pm

It's much easier to copy the string I provided and simply replace the table name using PHPMyAdmin. You can grab the matching table names using this:

SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('columnA','ColumnB') AND TABLE_SCHEMA='YourDatabase';

Easy-peasy, no need for any PHP to achieve this.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by straightlight » Sat Oct 09, 2021 9:47 pm

thekrotek wrote:
Sat Oct 09, 2021 7:58 pm
It's much easier to copy the string I provided and simply replace the table name using PHPMyAdmin. You can grab the matching table names using this:

SELECT DISTINCT TABLE_NAME FROM INFORMATION_SCHEMA.COLUMNS WHERE COLUMN_NAME IN ('columnA','ColumnB') AND TABLE_SCHEMA='YourDatabase';

Easy-peasy, no need for any PHP to achieve this.
However, your query is only pulling the data, it does not write to it while the OP's request is about updating the date_added field.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by thekrotek » Sat Oct 09, 2021 10:03 pm

straightlight wrote:
Sat Oct 09, 2021 9:47 pm
However, your query is only pulling the data, it does not write to it while the OP's request is about updating the date_added field.
I already provided the string to update the field earlier.

Professional OpenCart extensions, support and custom work.
Contact me via email or Skype by support@thekrotek.com


User avatar
Expert Member

Posts

Joined
Sun Jul 03, 2016 12:24 am


Post by straightlight » Sat Oct 09, 2021 10:05 pm

thekrotek wrote:
Sat Oct 09, 2021 10:03 pm
straightlight wrote:
Sat Oct 09, 2021 9:47 pm
However, your query is only pulling the data, it does not write to it while the OP's request is about updating the date_added field.
I already provided the string to update the field earlier.
Correct, you provided the string for one table already aside from the rest.

Dedication and passion goes to those who are able to push and merge a project.

Regards,
Straightlight
Programmer / Opencart Tester


Legendary Member

Posts

Joined
Mon Nov 14, 2011 11:38 pm
Location - Canada, ON

Post by paulfeakins » Mon Oct 11, 2021 7:18 pm

imager wrote:
Fri Oct 08, 2021 11:00 pm
In my MySQL Database, I would like to update the date_added field to the current date for ALL records.
Why?

UK OpenCart Hosting | OpenCart Audits | OpenCart Support - please email info@antropy.co.uk


User avatar
Legendary Member
Online

Posts

Joined
Mon Aug 22, 2011 11:01 pm
Location - London Gatwick, United Kingdom
Who is online

Users browsing this forum: No registered users and 28 guests