| | |
Exclusionary SELECT in SQL
Please support our MySQL advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Jun 2005
Posts: 14
Reputation:
Solved Threads: 0
Hi,
While I'm not entirely new with SQL, I am not by any means highly knowledgable.
I'm attempting to write a SQL statement to do the following, and having no luck:
Assumptions:
There is only one table, with several fields of metadata including the following.
1 field (integer) that indicates which section the table element is in. (e.g., 1="Illustrations Gallery" 2="Painting Gallery") Call this Field A.
1 field that can be NULL, or can contain the id (pkey) of another entry in the same table indicating a link between them. Call this Field B.
Field B can only contain links (primary keys) from elements that have a specific value of Field A.
I want to select the IDs of "eligable" elements. That is, rows who have the right value of A, and are not present in any other row's value of B.
Therefore: I need a query that says, essentially -
SELECT * from `table` where `A`=X AND `id`=(Doesn't Exist Anywhere in B);
Any ideas?
--
Aiden
While I'm not entirely new with SQL, I am not by any means highly knowledgable.
I'm attempting to write a SQL statement to do the following, and having no luck:
Assumptions:
There is only one table, with several fields of metadata including the following.
1 field (integer) that indicates which section the table element is in. (e.g., 1="Illustrations Gallery" 2="Painting Gallery") Call this Field A.
1 field that can be NULL, or can contain the id (pkey) of another entry in the same table indicating a link between them. Call this Field B.
Field B can only contain links (primary keys) from elements that have a specific value of Field A.
I want to select the IDs of "eligable" elements. That is, rows who have the right value of A, and are not present in any other row's value of B.
Therefore: I need a query that says, essentially -
SELECT * from `table` where `A`=X AND `id`=(Doesn't Exist Anywhere in B);
Any ideas?
--
Aiden
What version of MySql? 4.1 and newer support subqueries. If your version support subqueries, you can do something like this:
Otherwise, try this:
That second query was not fun! I got help at this URL:
http://dev.mysql.com/doc/refman/4.1/...ubqueries.html
MySQL Syntax (Toggle Plain Text)
SELECT * FROM mytable WHERE a = 147 AND id NOT IN (SELECT b FROM mytable)
Otherwise, try this:
MySQL Syntax (Toggle Plain Text)
SELECT t1.* FROM mytable t1 LEFT JOIN mytable t2 ON t1.id = t2.b WHERE t1.a = 147 AND t2.b IS NULL
That second query was not fun! I got help at this URL:
http://dev.mysql.com/doc/refman/4.1/...ubqueries.html
![]() |
Similar Threads
- Error in creating ODBC connection."Reason: Not assosiated with a trusted SQL Server" (ASP)
- Best way to develop db for survey? (Database Design)
- Hi,Need help on sql select statement structure (MS SQL)
- Command text was not set for the command object. (ASP)
- select statemt LIKE (MySQL)
Other Threads in the MySQL Forum
- Previous Thread: query_cache_size
- Next Thread: show status like '%qcache%';
| Thread Tools | Search this Thread |
agplv3 alfresco amazon api artisticlicense aws bizspark breathalyzer camparingtocolumns changingprices cmg communityjournalism contentmanagement contractors copyright count court crm database design developer development distinct drupal dui ec2 email enterprise eudora facebook form foss gartner gnu government gpl greenit groupware hiring hyperic images innerjoins insert ip joebrockmeier join keyword keywords kickfire laptop law legal license licensing linux maintenance managing mariadb matchingcolumns metron micromanage microsoft microsoftexchange mindtouch montywidenius mozilla multiple music mysql mysqlcolumnupdating mysqldatetimeordermax() mysqlindex mysqlinternalqueries mysqlquery mysqlsearch news open-xchange opendatabasealliance opengovernment opensource oracle penelope priceupdating query referencedesign reorderingcolumns resultset saas select sharepoint simpledb sourcecode spotify sql sugarcrm syntax techsupport thunderbird transparency virtualization







