3

What is wrong with my sql statement, it says that the problem is near the FULL JOIN, but I'm stumped:

SELECT `o`.`name` AS `offername`, `m`.`name` AS `merchantName` 
FROM `offer` AS `o` 
FULL JOIN `offerorder` AS `of` ON of.offerId = o.id 
INNER JOIN `merchant` AS `m` ON o.merchantId = m.id 
GROUP BY `of`.`merchantId` 

Please be gentle, as I am not a sql fundi

9
  • 2
    fundi = "the base of a hollow organ or that part of the organ farthest from its opening" Commented Jan 25, 2011 at 20:59
  • 3
    MySQL doesn't support full joins, see stackoverflow.com/questions/2384298/… Commented Jan 25, 2011 at 20:59
  • 1
    I don't see the need for the OFFERORDER reference, unless you need merchants in there (which doesn't require the need for FULL JOIN, even if MySQL supported the syntax). Also, backticks are only necessary for escaping MySQL keywords - which none of the above are. Commented Jan 25, 2011 at 21:03
  • 2
    Those errors are good, because they educate people to properly name tables and columns. There's no value in hiding that from those who are learning. Commented Jan 25, 2011 at 21:29
  • 1
    thank you all for all your comments and answers! Commented Jan 26, 2011 at 9:59

1 Answer 1

3

MySQL doesn't offer full join, you can either use

  • a pair of LEFT+RIGHT and UNION; or
  • use a triplet of LEFT, RIGHT and INNER and UNION ALL

The query is also very wrong, because you have a GROUP BY but your SELECT columns are not aggregates.

After you convert this properly to LEFT + RIGHT + UNION, you still have the issue of getting an offername and merchantname from any random record per each distinct of.merchantid, and not even necessarily from the same record.

Because you have an INNER JOIN condition against o.merchant, the FULL JOIN is not necessary since "offerorder" records with no match in "offer" will fail the INNER JOIN. That turns it into a LEFT JOIN (optional). Because you are grouping on of.merchantid, any missing offerorder records will be grouped together under "NULL" as merchantid.

This is a query that will work, for each merchantid, it will show just one offer that the merchant made (the one with the first name when sorted in lexicographical order).

SELECT MIN(o.name) AS offername, m.name AS merchantName 
FROM offer AS o 
LEFT JOIN offerorder AS `of` ON `of`.offerId = o.id 
INNER JOIN merchant AS m ON o.merchantId = m.id 
GROUP BY `of`.merchantId, m.name

Note: The join o.merchantid = m.id is highly suspect. Did you mean of.merchantid = m.id? If that is the case, change the LEFT to RIGHT join.

Sign up to request clarification or add additional context in comments.

5 Comments

Since when is an answer just a load of questions for the OP to clarify? This is a comment, not an answer.
@OMG I don't see any question marks - can you point them out? I am pointing out flaws in the SQL to help the asker progress in learning SQL.
Discussion but no example == OP questions "how do I do that?" If you won't answer, the community will treat you appropriately.
Why does it take threat of public shaming for you to give a real answer?
@omg Check all 3 revisions. Each is a valid answer. Feel free to check my answers on other questions - no "threats" required; I just improve on answers when I see more detail.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.