I have 2 tables :
PRODUCTS
+----------+----------+----------+----------+
| id | title | content | city |
+----------+----------+----------+----------+
CITIES
+----------+----------+
| id | title |
+----------+----------+
Where in my products table data stored like : (cities that a product belongs to separated by comma)
PRODUCTS
+----------+----------+----------+----------+
| id | title | content | city |
+----------+----------+----------+----------+
| 1 | some | info | 1,3,4,15
Now , how can i select products and all cities that it belongs to. And i want to search products by city's id too.
I've read some topics also this one , but mine is a bit different
Best Answer
I have a function that I keep on the master database for these cases. I will start by creating my function as per code below:
then I will create my tables according to your question. I did not bother about indexes here, but you shoudl have a look at this, because this process is bound to be very process intensive.
this gives me the following tables:
then, the final step, I use CROSS APPLY with the function, as you can see on the code below:
and that gives the following result, which I believe is what you are looking for: