I have a MySQL database with two tables. The first is a hierachy of departments, whereby each record has a parent record from within the same table, and the top level has a parent_id of 0.
The second is a table of products. Each product can live anywhere in the tree of departments.
I need to be able to let people search for a product by specifying a department to search within, but for the search to look within all the sub-departments of the specified department. Can this be done in a single query?
My "Plan B" is to store a list of parent ids with each product in a new field, and search that field using a LIKE query, but that seems nasty somehow.
Dummy data:
id|parent_id|name
1|0|Main 1
2|0|Main 2
3|0|Main 3
4|1|Sub 1-1
5|1|Sub 1-2
6|1|Sub 1-3
7|4|Sub 1-1-1
8|4|Sub 1-1-2
9|4|Sub 1-1-3
id|department|product
1|1|test product 1
2|4|test product 2
3|7|test product 3
SELECT * FROM products WHERE department = 1 ; want to receive all three products
SELECT * FROM products WHERE department = 4 ; want to receive products 2 and 3
SELECT * FROM products WHERE department = 7 ; want to receive only product 3
Related
So im trying to get a table that shows 3 activities which have the id's of 6,7,8 and they must be from a specific country. It should show their names and the number of times they appear in the person_activity_interval but the table i get is only 1 row with the name of 1 activity and the count of all the activities that are listed. If i remove other 2 activities it gets me the correct name and count but i need a table with all 3. Do i need to use something like join or union?
SELECT name,count(activity_id)
FROM activity,person_activity_interval
WHERE oib IN (SELECT oib
FROM person
WHERE living_id IN (SELECT id
FROM living
WHERE country= "Poland"))
AND ((activity_id=8 AND id =8) OR (activity_id=7 AND id =7) OR (activity_id=6 AND id =6));
I have a table with ~6500 products and images in which each product can have several jpg files:
ID_product product product_photo ordering
1 Product A a1234.jpg 1
2 Product B x5678.jpg 0
3 Product A b1234.jpg 0
4 Product B y5678.jpg 1
5 Product B z5678.jpg 2
6 Product C e4455.jpg 1
7 Product C f4455.jpg 0
8 Product C g4455.jpg 2
So I created a query in MS ACCESS:
TRANSFORM First([table1].product_photo) AS product
SELECT [table1].ID_product, First([table1].product) AS products
FROM [table1]
GROUP BY [table1].ID_product
PIVOT [table1].product_photo;
The result of the query:
ID_product product a1234_jpg b1234_jpg e4455_jpg f4455_jpg g4455_jpg x5678_jpg y5678_jpg z5678_jpg
1 Product A a1234.jpg b1234.jpg
2 Product B x5678.jpg y5678.jpg z5678.jpg
3 Product C e4455.jpg f4455.jpg g4455.jpg
I would like to change the table in such a way that the images are in columns:
ID_product product image_1 image_2 image_3
1 Product A a1234.jpg b1234.jpg
2 Product B x5678.jpg y5678.jpg z5678.jpg
3 Product C e4455.jpg f4455.jpg g4455.jpg
How to expand the query to get the desired result?
Because the Pivot works on the value of the column rather than its name, you are out of luck. This is the problem with pivoting - it's fairly limited in its flexibility.
At this point, you usually revert to code. For example, you could build the result set in VBA in an array of arrays of variant.
Or, you could use VBA to count the maximum number of columns for any product, create a temp table with appropriately named columns, and insert the values into the appropriate rows.
Either way, this is really a UI issue of sexy display to the user, rather than handing off data from one query to the next, which is what SQL is designed for.
I am working on module development for Prestashop. Now I have situation in which I have to fetch all products which have selected attributes. There is a interface, where dropdown list of all active attributes are showing. And user does selects attributes as per need. Now, I want to find all the products on the basis of selected attributes.
Below are the table structure:
Product Table:
id_product id_shop ean upc quantity price
1 1 abc 50 16.99
2 1 def 25 25.99
Product Combination Table
id_attribute id_product
1 1
13 1
5 1
1 2
10 2
Can anyone please help on how can I fetch products on the basis of selected attributes??
Isn't this really basic My SQL ?
select * from product
where
(
select count(*) from
product_combination
where product_combination.id_attribute in (X,Y,Z)
and product.product_id = product_combination.product_id
) = 3
where X,Y,Z are the attributes the user selected, and 3 is the count of attributes selected.
If this is more involved, I think you need to edit your question to provide some more details as to exactly what the technical issue is. If its just that you dont know SQL then this isn't really the place to post your query.
My database structure looks something as follows:
Database Name: Products
id ptype
1 Cups
2 Shirts
3 Cups
4 Mugs
5 Fabric
6 Mat
Database Name: Categories
id category ptype
1 Clothes Pants, Shirts, Tshirts
2 Other Mugs, Cups
I want to get a list of distinct ptype (products table) which aren't already listed in ptype of categories table
So the result would be
ptype
Fabric
Mat
I tried using the following mysql select query but it doesn't seem to work
SELECT p.ptype, c.ptype, FIND_IN_SET(p.ptype,c.ptype) FROM products as p, categories as c WHERE FIND_IN_SET(p.ptype,c.ptype) < 1
It returns value of FIND_IN_SET as 0 for all the ptypes of products table.
Remove the spaces after the commas in categories.ptype field to make the query work. find_in_set() compares the needle to a list of comma separated values and considers the space after the commas to be part of the strings it searches, therefore no match is found if you search for a string that does not contain the spaces.
I am designing a database which relates products and the stores which carry them.
Many of the stores will carry the same products as each other.
As well, obviously, there will be many types of products per store.
I tried to do some research on the best way to make the table but all the resources i read online are so technically exhausting and logically formalized that it takes away from ability to intuitively understand what they're saying.
I keep hearing people say that you should use a junction table, but I dont understand how that would be applicable in my case.
What i wanted to do was create one row for "Store" category, and then have separate rows "product_1", "product_2" "product_x" etc. where i would list each of the products the store sells, but i'm not sure if that's good form.
Furthermore, some stores might have 50 products, meaning i would need 50 rows. I'm not sure if there's a way around this or not.
Ultimately, my question is, by standard conventions, what is the ideal database structure for this kind of relationship?
I would have three tables. One for stores [name, id & info]. One for items [name, id & info]. One to link the two [relation id, store id & item id].
This would enable you to change store and item data without having to update your relationships table.
Thus if you want items in a store you search the relation table for items that match the store id. Vice versa if you want stores that have an item, you search the relation table for the item id and return the store id's that carry that item.
Bare necessities Example:
STORE:
*********************
storeID storeName
*********************
1 store1
2 store2
3 store3
ITEMS:
*********************
itemID itemName
*********************
1 item1
2 item2
3 item3
RELATIONS:
*********************
storeID itemID
*********************
1 1
1 2
2 1
2 3
3 2
3 3
Thus to find which items store1 has you would get the storeID which is 1, and search the relations table to find which rows have a store ID = 1, which would return rows one and two, which tells you that store1 has items 1 and 2. Similarly to find which stores cary item2 you would get the ID of item2, which is 2, search the relations table for itemID = 2, which would return rows two and five, which tells you that stores 1 and 3 have item2.