MySQL Query returns duplicate results

I am new to MySQL and I am working with a database system that contains four main tables described here:

http://www.pastie.org/3832181

The table this query works with is here:

http://www.pastie.org/3832184

Seems pretty simple?

The purpose of my query is to grab all the BusinessID for the explicit user, where OpportunityID is NULL, after it has these BusinessIDs, I want it to find the associated BusinessName in the Business table and match this BusinessName with the name BusinessName (Business ) in the EmploymentOpportunity table.

This is my request for this action.

SELECT EmploymentOpportunity.OpportunityID, Business, Description 
FROM UserBusinessOpportunity, Business, EmploymentOpportunity
WHERE UserBusinessOpportunity.BusinessID = 
          (SELECT UserBusinessOpportunity.BusinessID 
          FROM UserBusinessOpportunity 
          WHERE UserBusinessOpportunity.UserID=1 AND
                UserBusinessOpportunity.OpportunityID is NULL) 
       AND UserBusinessOpportunity.BusinessID = Business.BusinessID 
       AND Business.BusinessName = EmploymentOpportunity.Business;

BusinessID. , , , , . 3, 24 8 3.

, .

+5
3

distinct .

+2

-, SQL-. , , , .

SELECT 
    User.UserID,
    Business.BusinessID,
    Business.BusinessName
FROM
    User,
    UserBusinessOpportunity,
    Business
WHERE
    User.UserID = 1 AND
    User.UserID = UserBusinessOpportunity.UserID AND
    UserBusinessOpportunity.OpportunityID IS NULL AND
    UserBusinessOpportunity.BusinessID = Business.BusinessID 
+1

, , .

.

:

SELECT EmploymentOpportunity.OpportunityID, Business, Description
FROM EmploymentOpportunity
JOIN Business ON Business.BusinessName = EmploymentOpportunity.Business
LEFT JOIN UserBusinessOpportunity USING(BusinessID) 
WHERE
 UserBusinessOpportunity.UserID=1
AND
 UserBusinessOpportunity.OpportunityID is NULL

Julian

+1
source

All Articles