How to change Customer Id and Order ID? - php

Currently CustomerID's start at 1 while the first Order generated has OrderID 100000001.
Is there any way of altering these fields, so the first customer created has
customer number 900000001 and the first order created has OrderID 900000001?

By changing the increment_last_id in eav_entity_store table, you can control the starting number of customer number, Order Number, Invoice Number, and shipping Memo Id.

Paste this code to Phpmyadmin to change the Order Number
UPDATE eav_entity_store
INNER JOIN eav_entity_type ON eav_entity_type.entity_type_id = eav_entity_store.entity_type_id
SET eav_entity_store.increment_last_id='XXXXX'
WHERE eav_entity_type.entity_type_code='order';
Replace the X‘s with your desired order number
for more detail you can go to this link for that:
click here

You may try to alter the AUTO_INCREMENT of the table before adding new customer or order:
$magento_db = Mage::getSingleton('core/resource')->getConnection('core_write');
$magento_db->query("ALTER TABLE customer_entity AUTO_INCREMENT = ".$desiredId);

Related

delete item that are not present on another database

I tried this code to delete the item that not present on another database what should i do ?
mysqli_query($link,"delete from payment where INVOICE_NO_MX NOT IN(select * from invoice) ");
You should be comparing INVOICE_NO_MX against the correct column in invoice. Something like this:
DELETE
FROM payment
WHERE INVOICE_NO_MX NOT IN (SELECT INVOICE_NO_MX FROM invoice)
^^^ replace with appropriate column name
Please replace * with appropriate field name.
select '*' from invoice.
This query returns multiple columns but in sub-query you can return only 1 column or summary functions.
Hope this will solve your problem.

Adding database entries together

I am making a site which allows admins to basically add points for a user.
At this point in time, I have a table, where id_child is unique, and id_points changes. So a constant stream of id_points can come in, however, it will only show the latest id_points, not the total.
I am wondering how I could go about creating a PHP script that could add all of those together.
From the image, the idea is that I want all id_points values added together to give a total, and this is for the same id_child
Use SQL sum() funciton:
select sum(id_points) from table `table_name` where `id_child` = 1
Hope i understood right.
First if you want to show only the latest points added you have to create another table #__points where you will keep every new change of points.
You need 3 columns id as PRIMARY and AUTO_INCRENMENT , pts and user_id . user_id will be FK to id_child.
So when you want to add a new record :
INSERT INTO `#__points` (pts,user_id) VALUES ("$pts",$id)
When you want to select last inserted value for each admin :
SELECT * from `#__points` where user_id=$id ORDER BY id ASC LIMIT 1

Unique Columns in SQL Views

In my database I have one table that contains a complete list of products, and another table that contains the same list of products on the x-axis, with a list of customers on the y-axis, where the value for each product can be 1 or 0 depending on whether that customer can view that product. My SQL looks like this:
SELECT products.product_code, products.product_type, products.product_category, products.product_title, products.product_description
FROM product_lists
INNER JOIN products
ON product_lists.product_code=products.product_code
WHERE product_lists.customer="1"
ORDER BY products.product_code
My problem is that I would like to create a view of this result for each customer to use as that customers product table, however when I create it I get the message "This table does not contain a unique column. Grid edit, checkbox, Edit, Copy and Delete features are not available." even though the product_code field is set as a primary key in both the products table and the product_lists table.
How can I create a join/view that uses the primary key from the table(s) it was created from? In short I would like the product_code field to become the primary key of my view.
Thanks!
I think the problem is the join. You can fix this by moving the condition to the where clause. MySQL doesn't allow subqueries in the from, but it does in the where:
SELECT p.product_code, p.product_type, p.product_category, p.product_title, p.product_description
FROM products p
WHERE EXISTS (SELECT 1
FROM product_lists pl
WHERE pl.product_code = p.product_code AND
pl.customer = 1
)
ORDER BY p.product_code;

Incrementing table column for order entry

I have a table that stores orders.
id (auto increment) an oid (not auto incremented) and then the order details.
some orders can have multiple entries, this is why i have an ID and an Oid(order id)
so if you order 3 xwidgets and 4Zwidgets they go under the same OID
I am using the following code to assign the next OID number and am wondering if this is the best way:
$maxquery = mysql_query("SELECT MAX(oid) FROM ordr") or die(mysql_error());
$maxresult = mysql_fetch_array($maxquery);
$maxid = $maxresult['MAX(oid)'];
$oid = $maxid + 1; -- this gives me a new OID for the next entry since i can not auto increment this column
You probably want to use auto-increment columns. This will let you insert the data, and MySQL will pick an index for you. You'll need to denormalize your data to do this though.
You'll want two tables: orders and order_items, where:
orders
------
order_id
[any other columns]
order_items
-----------
item_id
order_id
[any other columns]
Insert the order into the orders table with a NULL order_id and record the order_id that MySQL sets. Now insert the order_item using the order_id you have, and let it auto-generate the item_id.
This way, MySQL will generate the numbers, so you don't need to worry about two orders trying to use the same order ID. Also, any order information won't be duplicated for each item in the order.

Mysql insert current result and then insert new result

I have an ajax live table edit to change the price of my current product. What I want to do is insert the price before I change it and then insert the updated price. The reason being is because I want to show the change in the updated price. Example: current price is $54.00 and I change it to $57.00. I need to keep a log of price change throughout the day and show the price change of $3.00. How would I go about inserting the old price while the updated price gets inserted also. Thanks.
I suggest you make your price table like this
table price
-----------
id unsigned integer autoincrement primary key
article_id integer /*link to articletable*/
valid_from date
valid_until date
amount decimal(10,2) /*always use decimal for money*/
Then you can insert your new price using the following 4 queries.
/*hide changes from the rest of the world until we are done*/
START TRANSACTION
/*invalidate the latest existing price in the price table*/
UPDATE price
SET valid_until = DATESUB(CURDATE(),INTERVAL 1 DAY)
WHERE article_id = '100' ORDER BY valid_until DESC LIMIT 1
/*the order by selects the latest item, the limit does only 1 update*/
/*insert the new price*/
INSERT INTO PRICE (article_id, valid_from, valid_until, amount)
VALUES ('100', CURDATE(), DATEADD(CURDATE(),INTERVAL 100 YEAR), '99.95')
/*show changes to the rest of the world*/
COMMIT
You need the transaction or you risk the price table being out of sync. Set the tabletype to InnoDB on the price table.
Al your other tables can be MyISAM, just make sure the price table is InnoDB.
You can now select prices by using:
SELECT article.name
,price.amount as price_per_item
,purchase.qty as number_of_items
,price.amount * purchase.qty as amount
FROM purchase
INNER JOIN article ON (article.id = purchase.article_id)
INNER JOIN price ON (price.article_id = purchase.article_id)
AND (purchase.transactiondate BETWEEN price.valid_from and price.valid_until)
WHERE purchase.id = '458'
you can maintain to different fields for the two. Like old_value and new_value. At the end of the day you can tally the values and print the difference.

Categories