Tuesday, March 6, 2012

Adding new content to a table by refering other tables using SQL

I keep product name, id in Table1.

I keep Category name, id in Table2.

I keep relation between product and category (product_id, category_id) in Table3.

I have added some products to the table with proper category.

Work fine

But for some products I did not specified any category

(ie their id is not present in Table3)

But now I want all such products

(ie all products whose category is nothing)

To be associated with category_id 10

Can I do this simply with SQL queries?

Hope u can help me

sujith

Hellosujith!

UPDATE [TableName] -- in your case it will be TABLE3

SET [FieldName] = 10 -- in your case it will be CATEGORY_ID

WHERE [FieldName] IS NULL -- in your case i dont know if it is NULL or ''

http://www.w3schools.com take a look at this site to learn more about SQL STATEMENT

HAVE NICE QUERY'S

|||

But I don't have any entry in table 3 if the category of a product is nothing.

I insert data into table 3 if and only if a product , belonged to acategory(this is how I save space)

|||

ohhhh...

You want to see all products that doesn't has category_Id and isnert into table 3 with category_Id = 10!

Is this that you want?

Sorry if i am not understanding your question!!

|||

sujithukvl@.gmail.com:

I keep product name, id in Table1.

I keep Category name, id in Table2.

I keep relation between product and category (product_id, category_id) in Table3.

I have added some products to the table with proper category.

Work fine

But for some products I did not specified any category

(ie their id is not present in Table3)

But now I want all such products

(ie all products whose category is nothing)

To be associated with category_id 10

Can I do this simply with SQL queries?

Hope u can help me

sujith

Following Sql statement might suite your need:

insert into product_categoryselect id ,'10'fromproductwherenot exists (select *from product_categorywhere product_category.pro_id = product.id)

No comments:

Post a Comment