Update tables in database from tables in a different form - php

I have one working import xml to my mysql, which import to tables.
Now I want to update with these imported tables my new tables. But I don't know how.
The importer import from 3 different xml.
products, price, stock
It's import to products, product_descriptions, images, etc
Example:
Products
ID | Name | stock | date
1 | product 1 | 9999 | 2013.07.13
2 | product 2 | ....
Product_description
ID | description | price | date | sale price
1 | product 1... | 1$ | 2013.07.17 | 0$
2 | Product 2 is blabla.. | 999 $ | 2013... | 10$
I want to update with these items my another tables:
posts
Post ID | post title | description | product ID
100 | products-name | product_description-description | product-ID <- with these item from another tables.
and,
postmeta
post id | meta | value
100 | price | products_description - price
100 | sale price | product_description - sale price
100 | title | products-name
100 | image | imagepath
I don't know how can I do this.
Anyone help me?

You can use INSERT INTO ... SELECT construction.
There is a nice manual from MySQL: http://dev.mysql.com/doc/refman/5.0/en/insert-select.html
For instance,
INSERT INTO posts (post_title, description)
SELECT
Products.Name, Products.stock
FROM Products
This will insert to the posts table all names & stock levels of your products in a title and description columns
P.S. I do not see any connection between Products and Product Description. Are there any foreign keys?

Related

MySQL: join tables in such a way, so that the rows with the same foreign key would combine into one

Basically I have 2 tables:
One contains all of the products, the other one contains all of the categories for the products.
For one category there may be more than one input field for the category. Few examples:
+-------------+--------+
| SKU | WV2323 |
| Name | DVD |
| Price | 2.00 |
| >(category) | dvd |
| memory | 700MB |
+-------------+--------+
+-------------+-----------+
| SKU | bla3434 |
| Name | Chair |
| Price | 50.00 |
| >(category) | furniture |
| height | 70cm |
| width | 50cm |
| length | 100cm |
+-------------+-----------+
All the values for the categories are in the categories table, connected to product via product_id. So if there are 3 fields for the product category, they are contained as 3 different rows connected to the same product id. I need to fetch all of these rows as one, so that I could pass the multiple values for the category at the same time. Is this possible to do via SQL query?

Laravel eloquent table relationship for sorting data

I am trying to develop an e-commerce website in Laravel but I am having trouble with sorting the data of a category. I tried it through eloquent but unable to sort by price. I tell you my database table structure.
Table category
id | user_id | name | slug | description | timestamp
Table product
id | user_id | name | slug | content | tags | extras | feature_image | timstamp
Table category_product_relation
id | category_id | product_id | timestamp
Table product_price
id | product_id | price | tax | discount | quantity | timestamp
Now, I want to sort products of a category by price. Let me know if this db structure is good or not. If it is not good kindly guide me.

MySQL Select Product-SimilarProduct

I have two tables products and similar_products. Products tables holds data related to available products however this tables has loads of products that are same but with different product id. Which is why I have another table that hold the data for all the similar products. I need to run a query that selects products from products table but at the same also check in similar_products tables to not select any duplicate products.
So for example:
Products Table:
ProductID | Manufacturer | Part No | Name
-----------------------------------------------------------------
8202 | Hp | 402146-B21 | HP Auto Synch Cable
8210 | Hp | 113894-B21 | HP Stylus 3 Pack
8211 | Hp | 113894-B21 | HP Stylus 3 Pack
8212 | Hp | 113894-B21 | HP Stylus 3 Pack
Similar_products Table
ProductID | Similar_ProductID
----------|-------------------
8210 | 8211
8210 | 8212
8211 | 8210
8211 | 8212
8212 | 8210
8212 | 8211
How can I run a query that will only select ProductID 8202 and 8210 and not select duplicates products.
In case you always have the relationship x~y as the two records [x,y] and [y,x] in Similar_products your problem is a variation of The Rows Holding the Group-wise Maximum of a Certain Column
SELECT
p.ProductID, p.Name
FROM
Products as p
WHERE
NOT EXISTS(
SELECT
1
FROM
Similar_products s
WHERE
p.ProductID=s.ProductID
AND p.ProductID>s.Similar_ProductID
)

Laravel: how to make a key of a column name?

I'm bending my mind for some time now over this problem. Could someone please help me?
I have two tables: products and product_attributes. The product has all basic product information and product_attributes has all specific information for products on different categories. It's much like the magenta attribute system. this table has 4 columns: id, product_id, attribute_name, attribute_value.
Now let's say a product has 2 attributes:
------------------------------------------------------
| id | product_id | attribute_name | attribute_value |
------------------------------------------------------
| 1 | 123 | length | 123cm |
------------------------------------------------------
| 2 | 123 | material | Denim |
------------------------------------------------------
| 3 | 123 | season | Summer |
------------------------------------------------------
Now if I set up the eloquent relationships and query a product, I get a product object with all three attributes. So far this is what I wanted. But now in a blade template I would like to be able to do something like this:
$product->attribute->length
Is this even possible or do I need to achieve these kind of things with a total different approach (like creating different tables for different product types/categories)?
Thanks in advance!
length is a tuple value not an attribute you need
$product->attribute->where('attribute_name', 'length')
or
$product->attribute->whereAttributeName('length')

Tables structure for shop - items - prices

Suppose I have 15 shops, 4 products and custom prices for each shop:
each shop doesn't have all the 4 products but can have only 2, or 3 or 4 or even 1.
the prices are similar but customized to each shop. The prices change every 2-3 weeks in all shops, often simultaneously but not always.
the database must contain the historical of the prices.
This is my idea but I'm sure it lacks in the products section:
TABLE1
id | shop_name | product_1 | product_2 | product_3 | product_4
1 shop1 name2 name4 NULL NULL
2 shop2 name1 name3 name4 NULL
TABLE2
id | shop_name | active_from | active_to | name1 | name2 | name3 | name4
1 shop1 2014-01-05 2014-02-07 NULL price NULL price
2 shop2 2014-01-07 2014-02-05 price NULL price price
3 shop2 2014-02-06 NULL price NULL price price
4 shop1 2014-02-08 NULL NULL price NULL price
To get the data of the current prices I could select the rows where active_to is NULL and JOIN the tables to connect table1 with table 2.
To create an istorical chart or table I'll use the between active_from and active_to method.
But I'm limited to 4 products, what if they increase in the future?
I would try the following:
A table that contains the presence of each product in each shop and its price.
shop_id | product_id | price
1 | 1 | 10.23
1 | 2 | 13.51
1 | 3 | 324.12
2 | 1 | 13.20
2 | 3 | 9.80
In this example you see that shop 1 sells products 1, 2 and 3, while shop 2 sells products 1 and 3 only. This is the simple way to do it. It would be better, maybe, to use a table for storing which products are sold in which shops and another table for the prices, since they are different concepts.
Then another table for storing all the changes that have been made to the prices. With this table you can trace back all the price history.
shop_id | product_id | date
1 | 1 | 10.23
1 | 1 | 10.08
... | ... | ...
Whatever solution you take, do not create a column for each product unless you have some strange and very strong reason to do it.

Categories