繁体   English   中英

使用ORDER和LIMIT子句时SQL查询中的语法错误?

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

使用ORDER和LIMIT时存在语法错误。 我不熟悉mysql,您能否指出错误。

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

问题是products之间以及连接字符串的where之间没有空格,因此查询最终看起来像

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM