2017-03-17 15 views
0

使用ORDER和LIMIT時有語法錯誤。我不熟悉MySQL可以指出錯誤。SQL查詢中使用ORDER和LIMIT子句時的語法錯誤?

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"); 

回答

3

的問題是,你不必productswhere之間的空當您連接字符串,因此查詢最終看起來像

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

空間添加到字符串之一:

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"); 

你不會說你正在使用什麼編程語言,但許多允許字符串跨越行,所以你不需要連接。你可以寫:

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"); 
+0

我不好感謝btw快速幫助 – PVP