ilan Expert Licensed User Longtime User Dec 12, 2021 #1 Hi everyone, i need help with creating a query. i have 2 tables. Category and products. i have about 20 categories and each has an id. Now i have few products and every product belongs to a specific category with the category id. i want to list all categories ids that has at least 1 product with the same cat id. how can i do it? thanx
Hi everyone, i need help with creating a query. i have 2 tables. Category and products. i have about 20 categories and each has an id. Now i have few products and every product belongs to a specific category with the category id. i want to list all categories ids that has at least 1 product with the same cat id. how can i do it? thanx
RodM Member Licensed User Dec 12, 2021 #2 Hi, I'm not 100% sure if I understood, but... SELECT DISTINCT CATEGORY_ID FROM PRODUCTS would work?
ilan Expert Licensed User Longtime User Dec 12, 2021 #3 RMMIRON said: Hi, I'm not 100% sure if I understood, but... SELECT DISTINCT CATEGORY_ID FROM PRODUCTS would work? Click to expand... Yes, this would be a solution that i have already figured out but what i wanted is to use 2 tables and make the query on table 1 depending on query on table 2. i think i will need to use join here, right?
RMMIRON said: Hi, I'm not 100% sure if I understood, but... SELECT DISTINCT CATEGORY_ID FROM PRODUCTS would work? Click to expand... Yes, this would be a solution that i have already figured out but what i wanted is to use 2 tables and make the query on table 1 depending on query on table 2. i think i will need to use join here, right?
D Deleted member 103 Guest Dec 12, 2021 #4 ilan said: Yes, this would be a solution that i have already figured out but what i wanted is to use 2 tables and make the query on table 1 depending on query on table 2. i think i will need to use join here, right? Click to expand... you can use join or a second "SELECT DISTINCT ...", as it is easier for you.
ilan said: Yes, this would be a solution that i have already figured out but what i wanted is to use 2 tables and make the query on table 1 depending on query on table 2. i think i will need to use join here, right? Click to expand... you can use join or a second "SELECT DISTINCT ...", as it is easier for you.
josejad Expert Licensed User Longtime User Dec 12, 2021 #5 Try with Code: SELECT DISTINCT categories.id FROM categories INNER JOIN product ON categories.id = product.id_category
Try with Code: SELECT DISTINCT categories.id FROM categories INNER JOIN product ON categories.id = product.id_category
J JoseAlfredo Member Licensed User Dec 12, 2021 #6 B4X: SELECT b.IdCategory FROM PRODUCTS a INNER JOIN Categories b ON b.CategoryId = a.IdCategory
ilan Expert Licensed User Longtime User Dec 12, 2021 #7 José J. Aguilar said: Try with Code: SELECT DISTINCT categories.id FROM categories INNER JOIN product ON categories.id = product.id_category Click to expand... thank you very much, this is what i was looking for ? ? ? JoseAlfredo said: B4X: SELECT b.IdCategory FROM PRODUCTS a INNER JOIN Categories b ON b.CategoryId = a.IdCategory Click to expand... thanks, this will return duplicated results. adding distinct as @José J. Aguilar recommended will do the trick.
José J. Aguilar said: Try with Code: SELECT DISTINCT categories.id FROM categories INNER JOIN product ON categories.id = product.id_category Click to expand... thank you very much, this is what i was looking for ? ? ? JoseAlfredo said: B4X: SELECT b.IdCategory FROM PRODUCTS a INNER JOIN Categories b ON b.CategoryId = a.IdCategory Click to expand... thanks, this will return duplicated results. adding distinct as @José J. Aguilar recommended will do the trick.
TILogistic Expert Licensed User Longtime User Dec 12, 2021 #8 Normal SQL SQL: SELECT DISTINCT categories.id, categories.name FROM categories, products WHERE products.productId = ? (Id Cat) AND categories.id = products.Idcategory Count SQL: SELECT categories.id, categories.name, Count(*) FROM categories, products WHERE products.productId = ? (Id Cat) AND categories.id = products.Idcategory GROUP BY categories.id Last edited: Dec 12, 2021
Normal SQL SQL: SELECT DISTINCT categories.id, categories.name FROM categories, products WHERE products.productId = ? (Id Cat) AND categories.id = products.Idcategory Count SQL: SELECT categories.id, categories.name, Count(*) FROM categories, products WHERE products.productId = ? (Id Cat) AND categories.id = products.Idcategory GROUP BY categories.id
ilan Expert Licensed User Longtime User Dec 12, 2021 #9 oparra said: Normal SQL SQL: SELECT DICTINTC categories.id, categories.name FROM categories, products WHERE products.productId = ? (Id Cat) AND categories.id = products.Idcategory Count SQL: SELECT categories.id, categories.name, Count(*) FROM categories, products WHERE products.productId = ? (Id Cat) AND categories.id = products.Idcategory GROUP BY categories.id Click to expand... Thanx, thats very helpful ?
oparra said: Normal SQL SQL: SELECT DICTINTC categories.id, categories.name FROM categories, products WHERE products.productId = ? (Id Cat) AND categories.id = products.Idcategory Count SQL: SELECT categories.id, categories.name, Count(*) FROM categories, products WHERE products.productId = ? (Id Cat) AND categories.id = products.Idcategory GROUP BY categories.id Click to expand... Thanx, thats very helpful ?
TILogistic Expert Licensed User Longtime User Dec 12, 2021 #10 SQL Normal (ALL Data Base) Example SQLite demo DB. Chinook