Tuesday, August 28, 2018

Oracle vs MySql concat syntax

Was messing around with a MySql database today and was wondering why my query was turning up zero results when I know where should be results.  No error, just zero results returned.   Found out it was because of a difference in syntax when trying to concatenate.  This was the syntax I was using, which would work fine in Oracle:

SELECT yearID, AB, H, R, HR, RBI FROM baseball_batting b, baseball_players p
                   where yearID = '2017' and (p.nameFirst||' '||p.nameLast = '$name2' or p.nameGiven = '$name2') and b.playerID = p.playerID


Turns out I needed to use this:

SELECT yearID, AB, H, R, HR, RBI FROM baseball_batting b, baseball_players p
                   where yearID = '2017' and (concat(p.nameFirst,' ',p.nameLast) = '$name2' or p.nameGiven = '$name2') and b.playerID = p.playerID

So to break out the important part here:

Oracle:
p.nameFirst||' '||p.nameLast

MySql:
concat(p.nameFirst,' ',p.nameLast)