Solved

PHP MySQL Not Equal Comparison between Two Tables

Posted on 2007-11-16
7
2,915 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
Ransomware-A Revenue Bonanza for Service Providers

Ransomware – malware that gets on your customers’ computers, encrypts their data, and extorts a hefty ransom for the decryption keys – is a surging new threat.  The purpose of this eBook is to educate the reader about ransomware 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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Introduction Since I wrote the original article about Handling Date and Time in PHP and MySQL (http://www.experts-exchange.com/articles/201/Handling-Date-and-Time-in-PHP-and-MySQL.html) several years ago, it seemed like now was a good time to updat…
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.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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 …

770 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