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
)
Related
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?
I have tree tables:
table user_products
user_id | product_specific_id | order_no
1 | 1 | 1
1 | 2 | 1
1 | 3 | 2
table products_library
product_specific_id | product_id
1 | 3
2 | 3
3 | 1
table product_names
product_id | name
1 | prod1
2 | prod2
3 | prod3
Every product in the database does have unique product_id. But when user does order any product, he can modify it, so I created product_specific_id that I'm using in user_products, and table products_library where I can translate every unique product_specific_id to the base product_id.
Now every product_specific_id does have name of related product_id, that I do store in table product_names.
Now I need to display the name of every product_specific_id for specific user_idand order_no.
Expected result should look like this:
user_id | order_no | product_specific_id | name
1 | 1 | 1 | prod3
1 | 1 | 2 | prod3
I'm able to fit in two queries: first I'm selecting list of product_specific_id from user_products, and than I'm nesting SELECT like
SELECT name
FROM product_names
WHERE product_id IN (SELECT product_id
FROM products_library
WHERE product_specific_id IN (...)
But is it possible to fit everything it in one query? I have no idea how such thing could be achieved, if at all. Also, I'm not sure if nesting queries like this is good or not in the first place. Perhaps is it just fine to get it in two queries and nesting queries too much is bad idea?
You can try the below way using just JOIN
select user_id,order_no,u.product_specific_id,name
from user_products u join products_library p on u.product_specific_id=p.product_specific_id
join product_names pn on p.product_id=pn.product_id
where user_id=1 and order_no=1
Suppose I a table like this:
Categories:
| cat_id | cat_name |
|--------|----------|
| 1 | tvs |
| 2 | phones |
| 3 | tablets |
And then I have:
Products:
| product_id | product_name | product_category |
|------------|-----------------|------------------|
| 1 | tv sony | 1 |
| 2 | tv samsung | 1 |
| 3 | phone htc | 2 |
| 4 | phone motorolla | 2 |
| 5 | tablet apple | 3 |
And someone goes into index.php where I get 10 random products from the DB. For creating the link I'd have to fetch the database again (to get the name of the category) and then I could do something like mysite.com/phones/4 (that would be the link to phone motorolla).
Of course 10 wouldn't be too hard on the server but it's still another fetch on the database.
The other option is to fetch for the category name during the product creation, and generate a link there, something like:
| product_id | product_name | product_category | product_link |
|------------|-----------------|------------------|--------------|
| 1 | tv sony | 1 | tvs/1 |
| 2 | tv samsung | 1 | tvs/2 |
| 3 | phone htc | 2 | phones/3 |
| 4 | phone motorolla | 2 | phones/4 |
| 5 | tablet apple | 3 | tablets/5 |
However the name of the category may change and so the link wouldn't work anymore. So what should I do? Is there another way to do this?
Alright Nick so in SQL you have something very powerful called JOIN. A join in SQL allows you to merge columns from different tables based on a key. Usually the key is a primary key or a foreign key.
In your case, it seems you have as primary keys : cat_id and product_id
And as foreign key you have : product_category
Based on your database schema, you can query your tables like this :
SELECT p.product_id, p.product_name, c.cat_name
FROM PRODUCTS p INNER JOIN CATEGORIES c
ON p.product_category = c.cat_id
With this query you retrieve your product_id, product_name and cat_name based with only one query for your database.
Here is more info about joins in SQL => https://www.w3schools.com/sql/sql_join.asp
Hope it will help you.
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.
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?