Solved

PHP MySQL Not Equal Comparison between Two Tables

Posted on 2007-11-16
7
2,917 Views
Last Modified: 2010-04-21
I have two tables.  MySQL version 4.1.22-standard -  PHP 5.2.4.  

I'm trying to get the data from the Review Table that do not have entries in the Comment Table.  

Here's what I have..

First Table   Review - Fields ->  (item_id / date / user_id)  

Second Table Comments - Fields -> (comm_id, item_id, user_id,date)

Code Below.. Thanks..
SELECT Review.item_id, Review.date, Review.user_id, Comments.item_id
FROM Review, Comments
WHERE Review.item_id <> Comments.item_id

Open in new window

0
Comment
Question by:usiff
  • 4
  • 2
7 Comments
 
LVL 48

Accepted Solution

by:
hernst42 earned 500 total points
ID: 20296803
You can try the SQL below
SELECT Review.item_id, Review.date, Review.user_id
FROM Review LEFT OUTER JOIN Comments on (Review.item_id = Comments.item_id)
WHERE Comments.item_id IS NULL

Open in new window

0
 
LVL 7

Expert Comment

by:dansoto
ID: 20297975

SELECT Review.item_id, Review.date, Review.user_id, Comments.item_id
FROM Review
WHERE Review.item_id NOT IN 
(SELECT Comments.item_id from Comments); 

Open in new window

0
 

Author Comment

by:usiff
ID: 20299640
Hello hernst42:

Here is another quick issue..

When I add this code below is doesn't work...
WHERE Comments.item_id IS NULL AND Review.user_id = $_SESSION['user_login_id']

But this will work?
WHERE Comments.item_id IS NULL AND Review.user_id = '444'

What the heck I'm I missing?

Thanks

usiff
0
NFR key for Veeam Backup for Microsoft Office 365

Veeam is happy to provide a free NFR license (for 1 year, up to 10 users). This license allows for the non‑production use of Veeam Backup for Microsoft Office 365 in your home lab without any feature limitations.

 
LVL 48

Expert Comment

by:hernst42
ID: 20299795
maybe $_SESSION['user_login_id'] is empty and does not contain anything. What do ypu get when you print out var_dump($_SESSION['user_login_id']) before you create the SQL?
0
 

Author Comment

by:usiff
ID: 20300288
Hello hernst42,

It's printing our string(1) "444"

It's crazy..
0
 

Author Comment

by:usiff
ID: 20300316
It's working now..  Wow...  I was going crazy!!  

I love typosss

Thanks again!

0
 

Author Closing Comment

by:usiff
ID: 31409512
Thanks!
0

Featured Post

Three Reasons Why Backup is Strategic

Backup is strategic to your business because your data is strategic to your business. Without backup, your business will fail. This white paper explains why it is vital for you to design and immediately execute a backup strategy to protect 100 percent of your data.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Creating and Managing Databases with phpMyAdmin in cPanel.
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question