Solved

PHP MySQL Not Equal Comparison between Two Tables

Posted on 2007-11-16
7
2,912 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
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 
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

Control application downtime with dependency maps

Visualize the interdependencies between application components better with Applications Manager's automated application discovery and dependency mapping feature. Resolve performance issues faster by quickly isolating problematic components.

Join & Write a Comment

Creating and Managing Databases with phpMyAdmin in cPanel.
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
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 look for a specific file type in a local or remote server directory using PHP.

760 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now