I have two tables: 'Category' and 'Product'. In Category I have catid and cat name. In product table I have item-id, item-name, catid.
What I need to do is show a result that will have item name and category name. But the category name will be multiple. So that one product can have multiple categories on it.
You might want to create three tables, because of a join table to would allow each line to have multiple lines corresponding in the other table:
Category : catid catname
Product : itemid itemname
CategoryProduct : catid itemid
So a product can have 0, 1 or more Categories.
Example content for the join table, for two products having the same two categories:
catid itemid in CategoryProduct
1 3
1 4
2 3
2 4
If your schema has a category ID in the product table, then no, one produce can't have multiple categories. If you have another M:N table to link products to categories, you should update your question.
In reply to your comment:
The category id of the product table
is like 2,3,4 so 1 product can have
multiple category
If you're allowed to change the table structure, by all means follow KLE's advice. That's the sane and maintainable approach.
If you can't change the table structure, you can query the categories with a hack like this:
select *
from product p
inner join category c
on ',' + p.catid + ','
like '%,' + cast(c.catid as varchar) + ',%'
Related
I have a table called category, all the categories of my products are stored in it, worth the redundancy.
This is its structure
ID
Name
abbreviation
1
category 1
EX1
2
category 2
EX2
Transferring data from one program to another I find the products table that has the following structure
ID
Name
category
cell 1
Item for example 1
category 1
cell 3
item for example 2
category 2
What I want to do is that in the products table the data of the category field is replaced by the IDs of the category table, not by their names, how can I do this?
I have an idea of what the code should look like, but I don't know how to complete it correctly for this to work.
UPDATE products
SET category = select id from dbo.category where products
WHERE condition;
I have a 3 Tables like this:
Table 1: Category (ID, Description)
Table 2: SubCategory (ID, Description, CategoryParent_ID)
Table 3: Items (ID, SubCategory_ID, Info, Documentation, etc...)
where SubCategory_ID in items table refers to SubCategory Table, & this last one refers to Category Table by CategoryParent_ID.
enter image description here
I want to make a query, that:
When I select a Category from Table 1, Every item in Table 3 that related to this Category is shown (via SubCategory)
Example: I select IT Equipment from table 1
data shown must be: Every item in table 3 that is related to Table 2 AND Table 2 get its reference from TABLE 1
You probably want to look into joins.
SELECT *
FROM tableOne, tableTwo, tableThree
WHERE tableOne.ID = tableTwo.CategoryParent_ID AND tableTwo.ID = tableThree.SubCategory_ID
You can also avoid these joins in the "where" clause by explicitly placing them in the "from" clause.
For example (jarlh's way):
SELECT *
FROM tableOne
INNER JOIN tableTwo ON tableOne.ID = tableTwo.CategoryParent_ID
INNER JOIN tableThree ON tableTwo.ID = tableThree.SubCategory_ID
Here's some info:
https://www.w3schools.com/sql/sql_join.asp
I have a Oracle database that is not well normalized, so it allows free-form entry in a few fields that should be foreign keyed to a lookup table if designed better. For example an Items table looks like this:
TABLE: Items
ID | NAME | CATEGORY
1 foo cat1
2 bar cat2
3 baz weirdCat
...
The business has now decided that there are Approved Categories stored in a table like shown below, and in any query using Category, if the value IS NOT in the approved categories table it should show as 'Other'.
TABLE: ApprovedCategories
ID | CATEGORY
1 cat1
2 cat2
I have figured out how to replace the values using CASE but only if I hard code the category values in my select like this:
SELECT
id,
name,
CASE category
WHEN 'cat1' THEN category
WHEN 'cat2' THEN category
ELSE 'Other'
END AS category
FROM
Items;
In order to try to link it to the values in ApprovedCategories I have tried several thing like the code below but haven't found anything that works:
SELECT
id,
name,
CASE category
WHEN IN (SELECT category FROM ApprovedCategories) THEN category
ELSE 'Other'
END AS category
FROM
Items;
Note: the requirements do not allow me to just update the values in the items table. I have to retain the original category entries in that table for now, even if they are not in the ApprovedCategory table.
Using a left join to approved category table will be a good way to implement this. If a category does not exist, a.category will be null and subsequently replaced by "Other".
select coalesce(a.category, 'Other')
from Items i
left join ApprovedCategories a on(a.category = i.category);
How about:
CASE
WHEN EXISTS (
SELECT 1 FROM ApprovedCategories
WHERE ApprovedCategories.category = Items.category
)
THEN category
ELSE 'Other'
END AS category
I am writing a SQL statement for Oracle where there is a one to many relationship between two tables. The table Person has a foreign key to table Purchase which has a Purchase Description field.
I need to write a SELECT query that will take all the purchase records/rows and append them to each other like so
Person Table
PersonID PersonName
1 John
Purchases Table
PurchaseId (PK), PersonID(FK), PurchaseDescription
1 1 Book
2 1 Clothes
3 1 Bag
4 1 Dinner
So the output of the query would look like this
Output = 1, Book:Bag:Clothes:Dinner
The output will be one row from the one to many relationship where there are separate records for book, bag, clothes, and dinner.
Any help is appreciated. Thanks
to do this use a function called LISTAGG, like this
SELECT 'Output = '||CAST(P.PersonID AS VARCHAR(100)), LISTAGG(Pur.PurchaseDescription, ':')
FROM Person P
LEFT JOIN Purchase Pur ON P.PersonID = Pur.PersonID
GROUP BY P.PersonID
I have two columns in my products table, category1, and category2. In my categories table I have four different categories, if a product falls under that category, the category number comes under the category1 column, if it comes under another category at the same time, that category is entered into the category2 column, a product does not come under more than two category types simultaneously.
Here is an example:
productID | category1 | category2
1 | 1 |
2 | 1 | 2
3 | 2 |
So category type 1 will always be in the category1 column, and if the same product comes under category type 1 and 2 as shown above in product id 2, then this causes me problems when querying all products that are of category type 2.
Here is my first query where I want to retrieve all the products which come under category type 1:
SELECT * FROM products WHERE category1 = 1
My second query where I want to retrieve all the products which come under category type 2:
SELECT * FROM products WHERE category1 = 2 AND OR category2 = 2
I know the second query is wrong, I just wrote it as I need it to work.
I cannot figure out how to query two columns with one query, only by writing two separate queries, any suggestions is appreciated. I am using SQL Server 2008 so special SQL functions may not be supported.
I would use the following statement:
SELECT * FROM products WHERE category1 = 2 OR category2 = 2
This will select all rows that have value 2 in one (or both) of the columns. Furthermore you can use it for any value (changing the value, of course) including value 1. This makes it easy to create a prepared statement in your client software (if you have one) that uses just one variable.
It sounds like the following is what you want:
Select
*
From
Products
Where
Category1 = 2 or
Category2 = 2
However I think you may want to reconsider your table design. Whenever I see multiple simliar columns labels with numeric suffixes, I consider it a design smell.
An alternative is to create a seperate ProductCategories table with 2 columns: ProductID and Category. Your PK would be a compound key of both. If you used this design, the SQL you'd need to find all category 2 product would be:
Select
Products.*
From
Products
Inner Join
ProductCategories
On
Products.ProductID = ProductCategories.ProductID
Where
ProductCategories.Category = 2