匹配一个列值与mysql中的多个值

Ok, so I am creating a sort of online shopping service and I have a DB that requires a field to match up against multiple other fields. They need to be stored, so that they can be ignored and set as maybe for later viewing. I know that setting up a comma delimited field is usually unwise, but I cannot figure out a better way to keep track of the matches while being able to categorize them into separate columns later on.

For eg, I have 25 products to sell and have 10 cities in which I want to sell them. Some can be shipped to 5 cites and some can be shipped to 9 cities. Now if someone searches for products in one particular city, how will only those products appear as a search result that are available for sale in that city only?

Does anyone have any better way of doing this without the comma delimited field?

use a n:n Table:

Products Table

products

id | name

city table

cities

id | name

Mapping (unique constraint over BOTH columns, not single constraints):

shipping_information

product_id | city_id

Then you can easily select all available cities for a product, or select all products, which are shipped to a certain city.

All available cities for product 4: SELECT * FROM cities, shipping_info WHERE cities.id = shipping_info.city_id AND shipping_info.product_id = 4

All available products for city 3: SELECT * FROM products,shipping_info WHERE shipping_info.id = 3 AND shipping_info.product_id = products.id

I'd use about 3-4 tables:

  1. products - having data of each product
  2. cities - having data of relevant cities
  3. product_cities - having relation between product and city

If a product can be shipped to 5 cities, it would have 5 rows in product_cities containing the same product_id and different city_id.

When a person searches, have something like:

SELECT pr.*
FROM cities AS c
INNER JOIN product_cities AS pc
ON c.id = pc.city_id
INNER JOIN products AS pr
ON pc.product_id = pr.id
WHERE c.name LIKE 'New York'
Two way to you will be match one column value with mutiple value using 'case statement' or in statement in where clause like:

select (case when col1>col2 then col2 else
             when col1>col4 then col4 else 
             col1 end) as colname, col5 from tablename    

or

select * form tablename where col11 in (1,2,3)

Make three different tables - one for the products you have, one for the cities you want to sell them in and one for describing where each product can be send (id, product_id, city_id).

From there, just use simple joins to select only the possible products for the selected city.