3

I have two tables, both with only one column.

Table 1:  Merchants
Column:  merchant  (20 entries)

Table 2:  Categories
Column: category  (600 entries)

I'm trying to write a for each loop to select all entries within these two tables and list all possible combinations in this format. For example..

merchant1 category1
merchant1 category2
merchant1 category3... all the way to end of categories
merchant2 category1
merchant2 category2
merchant2 category3...all the way to end of categories
etc...

Should I use one mysql command to select the data, then use a for each loop?... or should I run 2 mysql commands and use a nested for each loop on multiple arrays?

3
  • Take cross product of the tables using full outer join Commented Jul 25, 2015 at 5:24
  • Could you show me a code example? Commented Jul 25, 2015 at 5:34
  • Sorry this isn't super constructive but I'd say "6 of one and half a dozen of the other". Commented Jul 25, 2015 at 5:36

3 Answers 3

3
SELECT `Merchants`.`merchant`, `Categories`.`category`
FROM `Merchants`
JOIN `Categories`;

Then do a foreach on the result.

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

Comments

2
SELECT table1.(put the star sign), table2.(put the star sign)
FROM table1
INNER JOIN table2 ON 1=1

Comments

1

use outer join,

SELECT * FROM Merchants, Categories    //Number of Records:20 X 600

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.