0

I have a factory table with factoryname: select factoryname from factory

I then have a products table: select productcode from products

I want to create a list that has products for all factory.

so output :

mill 1 product 1
mill 1 product 2
mill 1 product 3
mill 2 product 1
mill 2 product 2
mill 2 product 3
mill 3 product 1
mill 3 product 2
mill 3 product 3

I have something like this:

DECLARE @numrows int
DECLARE @i int
DECLARE @department int

 SET @numrows = (SELECT COUNT(*) FROM factory)
WHILE (@i <= @numrows)
    BEGIN
        SELECT factoryname,product FROM products,factory )
        SET @i = @i + 1
    END

I am obviously off the mark, here, any advice? Thanks as always.

1
  • why not join the two tables and order by factory ? Commented Feb 21, 2014 at 11:07

3 Answers 3

2

SQL is a set based language, so you should try not to use loops in SQL whereever possible. It will hurt performance.

What you want to do, is use a JOIN to JOIN 2 tables together:

SELECT factory.factoryname, products.productcode
FROM factory, products
INNER JOIN products.factory_id = factory.factory_id

Something like this. This will give you a collection of products that are made in a certain factory.

Displaying this in a nice way to the user should be done in your UI layer in your application. Do not try to use SQL to properly format data.

Sign up to request clarification or add additional context in comments.

Comments

1

If you want to list all the factory names and all products, you can use a cross join like this:

    select factoryname, productname from factory cross join products

A Cross Join is basically a Cartesian product of 2 tables, resulting in (m * n) records if table 1 has m and table 2 has n records.

3 Comments

No problems! But remember that this can quickly get ugly for tables with huge number of rows, even if only one of them is large.
This will give you, for every row in 'products', every row in factories. In fact, it will return 'number of rows in products' * 'number of rows in factories'. Not quite what the topicstarter had in mind, I guess ...
Possible, which is why my first line says 'If you want to list all the factory names and all products'. Not knowing if the sample output represented that condition or a filtered set, I took a guess at the requirement.
0

JOIN will be effective solution for the above but there should be common column in both the tables. INNER JOIN is most suitable if for all factories you have some product. LEFT JOIN will work if you want all factories to be listed though there are factories for which there is no product. RIGHT JOIN will work if you want all products to be listed and there are products for which there is no corresponding factory in factory table.

Hope it helps....someway..........

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.