سؤال

I have the following statement in JAVA:

String query = "SELECT H.Name as Name, R.RacesWon as Race FROM Horses AS H, Conditions AS C, Races AS R"+ "WHERE H.ID = R.HorseID AND C.ID = R.ConditionID";

I have connection to a SQL Azure database with 3 tables Horses, Conditions and Races.

I just made this join and I've got this exception:

com.microsoft.sqlserver.jdbc.SQLServerException: Incorrect syntax near 'H'.

I do not know why and I have tried every possibility with Horses, Races and Conditions instead of H,C,R with AS , without AS without success.

Furthermore I have run :

String query = "SELECT Name  FROM Horses WHERE ID = 1";

and was OK.

Any suggestions?

هل كانت مفيدة؟

المحلول

You have some concatenation issues. As @lrb notes in the comments:

R"+ "WHERE

Would equal RWHERE which would cause an error.

Also use SQL92 syntax instead of SQL89 with inner join

Instead try:

String query = "SELECT 
                   H.Name as Name,
                   R.RacesWon as Race 
                 FROM Conditions as C
                 inner join Races AS R on C.ID = R.ConditionID
                 inner join Horses AS H on R.HorseID = H.ID";
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top