![]() | |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
I have two tables and a join table. Product (id, name) Category (id, name) Content (id, product_id, category_id) I want to select all products that have some clause, say where name begins with A that are not already in a specified category. Something like, (pseudoquery) select p.* from Product as p join Content as cn on p.id=cn.product_id and category_id != XX I think what I need to do is something with a left join where product joins the content table and has a clause for the category_id and then selects all the rows that return null for the content, but this is really taxing my small brain. |
#3
| |||
| |||
|
|
"sks" <sks.remove.me.please (AT) magnum55 (DOT) remove11.co.uk> schrieb im Newsbeitrag news:carr1q$qbl$1 (AT) sparta (DOT) btinternet.com... I have two tables and a join table. Product (id, name) Category (id, name) Content (id, product_id, category_id) I want to select all products that have some clause, say where name begins with A that are not already in a specified category. Something like, (pseudoquery) select p.* from Product as p join Content as cn on p.id=cn.product_id and category_id != XX I think what I need to do is something with a left join where product joins the content table and has a clause for the category_id and then selects all the rows that return null for the content, but this is really taxing my small brain. SELECT p.* FROM Product p WHERE NOT EXISTS ( SELECT co.product_id FROM Content co, Category ca WHERE co.product_id = p.id AND co.category_id = ca.id AND ca.name = 'your category name' ) |
#4
| |||
| |||
|
|
I have two tables and a join table. Product (id, name) Category (id, name) Content (id, product_id, category_id) I want to select all products that have some clause, say where name begins with A that are not already in a specified category. Something like, (pseudoquery) select p.* from Product as p join Content as cn on p.id=cn.product_id and category_id != XX I think what I need to do is something with a left join where product joins the content table and has a clause for the category_id and then selects all the rows that return null for the content, but this is really taxing my small brain. Can anyone help ? |
#5
| |||
| |||
|
|
Ah, my bad. I completely forgot to mention I'm using Mysql and cannot use sub selects. |
#6
| |||
| |||
|
|
I want to select all products that have some clause, say where name begins with A that are not already in a specified category. |
#7
| |||
| |||
|
|
"sks" <sks.remove.me.please (AT) magnum55 (DOT) remove11.co.uk> wrote Ah, my bad. I completely forgot to mention I'm using Mysql and cannot use sub selects. select * from A where not exists (select * from B where A.id = B.id) is equivalent to select A.* from A left outer join B on A.id = B.id where B.id is NULL Standard technique for the MySQL encumbered :-) Regards, Kristian And in older versions of ORACLE (Yes, he's in MySQL land, but what the heck), |
#8
| |||
| |||
|
|
-- If a product can be in but one category then the following will do: SELECT P.product_id, P.product_name FROM Products AS P INNER JOIN ProductCategories AS PC ON P.product_name LIKE 'A%' AND -- constraint on products P.product_id = PC.product_id AND PC.category_id <> @category_id_specified -- If a product can be in multiple categories then try this: SELECT P.product_id, P.product_name FROM Products AS P LEFT OUTER JOIN ProductCategories AS PC ON P.product_id = PC.product_id AND PC.category_id = @category_id_specified WHERE P.product_name LIKE 'A%' AND -- constraint on products PC.category_id IS NULL The second query can handle both cases, that is, where a product is in a single category or multiple categories, but might be less efficient than the first query for single-category products. |
#9
| |||
| |||
|
|
Please post DDL, so that people do not have to guess what the keys, constraints, Declarative Referential Integrity, datatypes, etc. in your schema are. Sample data is also a good idea, along with clear specifications. |
|
You also do not seem to know that there is no such thing as a "magical, universal" id columns in RDBMS. However, there is a record locator in file systems and some vendors expose versions of these NON-RELATIONAL PHYSICAL LOCATORS and trap fools. Do no tbe fooled. |
![]() |
| Thread Tools | |
| Display Modes | |
| |