1

I have two tables country and state:

country
-----------
cid
countrycode
countryname

state
-------
sid
cid (fk)
statecode
statename

I am writing a join as:

SELECT s.sid, 
       s.statename, 
       c.countryname 
FROM   state s, 
       country c 
WHERE  s.cid = (+) c.cid 
       AND c.id = 1 

The above query gives a syntax error. Does MySQL not accept '+' symbol?

Kermit
  • 33,827
  • 13
  • 85
  • 121
seedi
  • 94
  • 10
  • 3
    Whats with the plus sign? – Mihai Oct 08 '13 at 12:54
  • 1
    @Mihai the plus sign is outer join . – seedi Oct 08 '13 at 13:05
  • @AnkitSharma the plus sign is outer join symbol. – seedi Oct 08 '13 at 13:06
  • 2
    `+` is a non-ANSI standard way of saying [`LEFT JOIN`](http://en.wikipedia.org/wiki/Join_(SQL)#Alternate_syntaxes). – Kermit Oct 08 '13 at 13:07
  • @AnkitSharma Just because you never used it, doesn't mean it's wrong. The world doesn't revolve around MySQL. – Kermit Oct 08 '13 at 13:10
  • (+) apparently is an atavism from the Jurassic http://stackoverflow.com/questions/1193654/difference-between-oracles-plus-notation-and-ansi-join-notation – Mihai Oct 08 '13 at 13:12
  • @Mihai Jurassic or not, some are still taught this notation or must use it. – Kermit Oct 08 '13 at 13:16
  • @AnkitSharma I don't understand what you're asking. – Kermit Oct 08 '13 at 13:16
  • @AnkitSharma [*...the + notation is only present for backwards compatibility because Oracle debuted it before the ANSI standard for joins was put in place.*](http://stackoverflow.com/questions/1193654/difference-between-oracles-plus-notation-and-ansi-join-notation) – Kermit Oct 08 '13 at 13:18
  • 1
    @Ankit he's saying that some people must use it because they are dealing with legacy code or old-school colleagues. – Aaron Bertrand Oct 08 '13 at 14:38
  • And the `WHERE s.cid = (+) c.cid` would yield an error in Oracle as well. The old Oracle syntax would be either `WHERE s.cid = c.cid (+)` (meaning: `s LEFT JOIN c`) or `WHERE s.cid (+) = c.cid` (meaning: `s RIGHT JOIN c`) – ypercubeᵀᴹ Oct 08 '13 at 14:48

4 Answers4

3

No, MySQL does not accept the + symbol. Aside from a few exceptions, MySQL uses ANSI standard JOIN syntax:

SELECT s.sid, 
       s.statename, 
       c.countryname 
FROM   state s 
       LEFT JOIN country c 
              ON c.id = s.cid 
WHERE  c.id = 1 
Kermit
  • 33,827
  • 13
  • 85
  • 121
0

Try this::

select 
s.sid,
s.statename,
c.countryName 
from country c 
LEFT JOIN state s on  s.cid = c.cid 
WHERE c.id=1
Sashi Kant
  • 13,277
  • 9
  • 44
  • 71
  • `+` is a non-ANSI standard way of saying [`LEFT JOIN`](http://en.wikipedia.org/wiki/Join_(SQL)#Alternate_syntaxes). – Kermit Oct 08 '13 at 13:09
0

Remove plus sign and . symbol at end of your query...

Select s.sid,s.statename,c.countryName from state s,
left join country c on s.cid = c.cid where c.id=1
bgs
  • 3,061
  • 7
  • 40
  • 58
  • `+` is a non-ANSI standard way of saying [`LEFT JOIN`](http://en.wikipedia.org/wiki/Join_(SQL)#Alternate_syntaxes). – Kermit Oct 08 '13 at 13:08
  • 1
    Ok.. But one more , am not recommend a outer join , but user wants to skip from the error, so only i suggest.. Thanks for comment.. – bgs Oct 08 '13 at 13:11
0

this will help you.

 SELECT s.sid, 
       s.statename, 
       c.countryname 
FROM   country c 
LEFT JOIN state s on (s.cid = c.cid )
where c.cid = 1;
Chamly Idunil
  • 1,848
  • 1
  • 18
  • 33
  • `+` is a non-ANSI standard way of saying [`LEFT JOIN`](http://en.wikipedia.org/wiki/Join_(SQL)#Alternate_syntaxes). – Kermit Oct 08 '13 at 13:07