Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 01-25-2013, 09:50 PM   PM User | #1
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
Query two tables where columns are not equal

I have set up a content recordset and a links recordset. A link name is included in the links recordset. It's easy to write a query that compares the list of possible link names to the content and displays all the names that have been used on the list by doing this:
PHP Code:
SELECT links.linkName
FROM links
content
WHERE links
.id content.linkID
ORDER BY links
.linkName ASC 
What I want is a list of the names NOT available. Everything I try either results in duplicates of the all names duplicated by the number of unused names or no results. Here's query that gives the list of used names duplicated by the number of unused names:
PHP Code:
SELECT links.linkName
FROM links
content
WHERE links
.id <> content.linkID
ORDER BY links
.linkName ASC 

I have tried all sorts of JOIN's and other logic. I must be missing something simple.

Last edited by rgEffects; 01-25-2013 at 11:55 PM.. Reason: Issue Resolved
rgEffects is offline   Reply With Quote
Old 01-25-2013, 10:09 PM   PM User | #2
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Code:
SELECT links.linkName
FROM links
LEFT JOIN content ON links.id = content.linkID
WHERE content.linkID IS NULL
ORDER BY links.linkName ASC
Try that. I'm terrible with null checking :P. I don't think MySQL support MINUS queries, but the above or using a subquery within the where should do the trick.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Users who have thanked Fou-Lu for this post:
rgEffects (01-25-2013)
Old 01-25-2013, 11:54 PM   PM User | #3
rgEffects
New Coder

 
Join Date: Aug 2012
Posts: 76
Thanks: 22
Thanked 0 Times in 0 Posts
rgEffects is an unknown quantity at this point
That did it. Never would have guessed that I'd have to put in WHERE content.linkID IS NULL and I didn't find anything like that in 40 minutes of searching...

Thanks again.
rgEffects is offline   Reply With Quote
Old 01-26-2013, 05:22 AM   PM User | #4
Fou-Lu
God Emperor


 
Fou-Lu's Avatar
 
Join Date: Sep 2002
Location: Saskatoon, Saskatchewan
Posts: 15,653
Thanks: 4
Thanked 2,451 Times in 2,420 Posts
Fou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to allFou-Lu is a name known to all
Yep. The concept is simple, if you drop the where clause and select the content.linkID as well, the left join guarantees all records from links, and only matching records from content. So only ones that have record in both would have content.linkID with a provided value. The rest would have null, and to remove the ones that are not null would give a list where content has no matching results to link.
__________________
As of PHP 5.5, the MySQL library has been officially deprecated. It is recommended to move to either MySQLi or PDO libraries for your mysql connectivity. See here for help choosing which interface you prefer: http://php.net/manual/en/mysqlinfo.api.choosing.php
Fou-Lu is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 05:13 AM.


Advertisement
Log in to turn off these ads.