php - Select id in multi varchar field -


table category
id int
description varchar

table products
id int
id_cat varchar
etc..

id | id_cat 1  | 1, 2, 3 2  | 3,4,6,2 3  | 1, 5 , 7 . . . 

i need

select * produts id_cat in ( '1','2' ) 

but not work!?

id | id_cat   1  | 1, 2, 3 

your database layout not allow having multiple categories per product -- allows 1 product category. need additional table hold product-category relations. also, likes expensive use, recommend using join instead.

try structure:

create table category (     id int primary key,     description varchar(255) );  create table product (     id int primary key,     category_id int,     description varchar(255) );  create table product_category (     product_id int,     category_id int ); 

you can join tables, using sql similar this:

select c.name "category name", p.name "product name" product p inner join productcategory pc on p.productid = pc.productid inner join category c on pc.categoryid = c.categoryid order c.name, p.name; 

for working example, see http://www.java2s.com/code/oracle/table-joins/getcategoriesandproductswithjoins.htm


Comments

Popular posts from this blog

resizing Telegram inline keyboard -

command line - How can a Python program background itself? -

php - "cURL error 28: Resolving timed out" on Wordpress on Azure App Service on Linux -