PVP
PVP

Reputation: 165

syntax error in sql query while using ORDER and LIMIT clause?

There is a syntax error while using ORDER and LIMIT. I am not familiar with mysql can you point out the error.

ps = con.prepareStatement("Select product_id,image_name,images.product_name,price,"
                       + "company_name from images,products"
                       + "where images.product_name = products.product_name ORDER BY hits DESC LIMIT 5");

Upvotes: 0

Views: 166

Answers (1)

Barmar
Barmar

Reputation: 781858

The problem is that you don't have a space between products and where when you concatenate the strings, so the query ends up looking like

... from images,productswhere images.product_name = ...

Add a space to one of the strings:

ps = con.prepareStatement("Select product_id,image_name,images.product_name,price,"
                       + "company_name from images,products"
                       + " where images.product_name = products.product_name ORDER BY hits DESC LIMIT 5");

You don't say what programming language you're using, but many allow strings to span lines, so you don't need the concatenation. You can then write:

ps = con.prepareStatement("Select product_id,image_name,images.product_name,price,company_name 
                        from images,products
                        where images.product_name = products.product_name
                        ORDER BY hits DESC LIMIT 5");

Upvotes: 3

Related Questions