mysql 关键字 desc_MySQL的desc关键字作为了字段名称查询问题

在数据库查询中遇到一个问题,当字段名为desc时,直接使用SQL语句`select...desc from...`会导致异常。原因是desc是MySQL的关键字。为了解决这个问题,可以使用别名来代替字段名,如`select e.id, e.name, e.price, ... e.desc from EntProduct as e where id = ?`,这样就能成功执行查询。" 124902985,10046616,IPSec隧道配置:预共享密钥认证的路由模式实践,"['网络', '安全', 'IPSec配置']
摘要由CSDN通过智能技术生成

引言:今天在开发的时候,由于业务需求,所有要在产品表的数据库中多查询一个字段(产品描述信息),字段名称是desc.

原本数据库查询语句如下:

" select id, name, price, martPrice, picturePath, firstImgUrl, smallImgUrl, stock, productNo, rootOrgId from EntProduct where id = ?"

新增字段后修改为如下:

" select id, name, price, martPrice, picturePath, firstImgUrl, smallImgUrl, stock, productNo, rootOrgId, desc from EntProduct where id = ?"可能很多人觉得这个语句没有问题,但是在执行查询的时候出现下面的异常信息:

java.lang.RuntimeException: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: desc near line 1, column 94 [select id,name,price,martPrice,picturePath,firstImgUrl,smallImgUrl,stock,productNo,rootOrgId,desc from com.product.vo.EntProduct where id=?]

org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: desc near line 1, column 94 [select id,name,price,martPrice,picturePath,firstImgUrl,smallImgUrl,stock,productNo,rootOrgId,desc from com.product.vo.EntProduct where id=?]

at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:54)

at org.hibernate.hql.internal.ast.QuerySyntaxException.convert(QuerySyntaxException.java:47)

at org.hibernate.hql.internal.ast.ErrorCounter.throwQueryException(ErrorCounter.java:79)

at org.hibernate.hql.internal.ast.QueryTranslatorImpl.parse(QueryTranslatorImpl.java:276)

at org.hibernate.hql.internal.ast.QueryTranslatorImpl.doCompile(QueryTranslatorImpl.java:180)

at org.hibernate.hql.internal.ast.QueryTranslatorImpl.compile(QueryTranslatorImpl.java:136)

at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:101)

at org.hibernate.engine.query.spi.HQLQueryPlan.(HQLQueryPlan.java:80)

at org.hibernate.engine.query.spi.QueryPlanCache.getHQLQueryPlan(QueryPlanCache.java:119)

at org.hibernate.internal.AbstractSessionImpl.getHQLQueryPlan(AbstractSessionImpl.java:214)

at org.hibernate.internal.AbstractSessionImpl.createQuery(AbstractSessionImpl.java:192)

at org.hibernate.internal.SessionImpl.createQuery(SessionImpl.java:1537)

at com.product.dao.EntProductDao.getProductByProductId(EntProductDao.java:2618)

at com.product.dao.LimitBuySetDao.getTopTen(LimitBuySetDao.java:238)

at com.product.manager.LimitBuySetManager.getOneByTime(LimitBuySetManager.java:714)

at com.alibaba.dubbo.common.bytecode.Wrapper21.invokeMethod(Wrapper21.java)

at com.alibaba.dubbo.rpc.proxy.javassist.JavassistProxyFactory$1.doInvoke(JavassistProxyFactory.java:46)

at com.alibaba.dubbo.rpc.proxy.AbstractProxyInvoker.invoke(AbstractProxyInvoker.java:72)

at com.alibaba.dubbo.rpc.protocol.InvokerWrapper.invoke(InvokerWrapper.java:53)

at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:64)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:60)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:112)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:108)

at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:84)

at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:170)

at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:52)

at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:82)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:745)

at com.alibaba.dubbo.rpc.filter.ExceptionFilter.invoke(ExceptionFilter.java:108)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.TimeoutFilter.invoke(TimeoutFilter.java:42)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.monitor.support.MonitorFilter.invoke(MonitorFilter.java:75)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.protocol.dubbo.filter.TraceFilter.invoke(TraceFilter.java:78)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.ContextFilter.invoke(ContextFilter.java:60)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.GenericFilter.invoke(GenericFilter.java:112)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.ClassLoaderFilter.invoke(ClassLoaderFilter.java:38)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.filter.EchoFilter.invoke(EchoFilter.java:38)

at com.alibaba.dubbo.rpc.protocol.ProtocolFilterWrapper$1.invoke(ProtocolFilterWrapper.java:91)

at com.alibaba.dubbo.rpc.protocol.dubbo.DubboProtocol$1.reply(DubboProtocol.java:108)

at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.handleRequest(HeaderExchangeHandler.java:84)

at com.alibaba.dubbo.remoting.exchange.support.header.HeaderExchangeHandler.received(HeaderExchangeHandler.java:170)

at com.alibaba.dubbo.remoting.transport.DecodeHandler.received(DecodeHandler.java:52)

at com.alibaba.dubbo.remoting.transport.dispatcher.ChannelEventRunnable.run(ChannelEventRunnable.java:82)

at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1145)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:615)

at java.lang.Thread.run(Thread.java:745)

错误信息应该很明显,就是新增的查询desc字段有问题。

其实出现这个错误的原因是desc这个单词是MySQL数据库的关键字,所有sql语句执行到这里的时候觉得语句有问题

但是由于这个字段名称是很多地方都用到的,所有不可能一一去修改,最后我用别名写sql语句

执行成功的sql语句如下:

" select e.id, e.name, e.price, e.martPrice, e.picturePath, e.firstImgUrl, e.smallImgUrl, e.stock, e.productNo, e.rootOrgId, e.desc from EntProduct as e where id = ?"

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值