golang db.Prepare select top ? o

2019-05-29  本文已影响0人  毛毛v5

用的这个驱动 _ "github.com/go-sql-driver/mysql"
经过测试,貌似golang的 db.Prepare在查询字符串中 不能使用 select top ? ... order by ? 这样的两个地方不能使用通配符啊。好奇怪。比如:
下面的代码就不行。

        sqlstr := `
            select top ?
            a.[Id], a.[CreateDate],a.[PublisherId],a.[NickName],a.[FileSize],a.[Followed],
            a.[Readed],a.[FilePath], b.[STitle],a.[CreateDate],a.[Encryptioned],a.[FileName]
            from [hds12204021_db].[dbo].[Web2019_historylist] a, [hds12204021_db].[dbo].[Web2019_roomlist] b
            where a.roomid = ? and a.Deleted = 0 and a.roomid=b.id and a.[CreateDate] < ?
            order by ? desc
        `
        log.Println(sqlstr)
        stmt1, err0 := db.Prepare(sqlstr)

如下错误:

2019/05/29 11:21:50 apis_public.go:343: 
            select top ?
            a.[Id], a.[CreateDate],a.[PublisherId],a.[NickName],a.[FileSize],a.[Followed],
            a.[Readed],a.[FilePath], b.[STitle],a.[CreateDate],a.[Encryptioned],a.[FileName]
            from [hds12204021_db].[dbo].[Web2019_historylist] a, [hds12204021_db].[dbo].[Web2019_roomlist] b
            where a.roomid = ? and a.Deleted = 0 and a.roomid=b.id and a.[CreateDate] < ?
            order by ? desc
        

2019/05/29 11:21:51 apis_public.go:346: ServeSrs sql db.Prepare error发生意外。 (语法错误或违反访问规则)


如果改成写死的参数,如下,就OK了。备注一下:

        sqlstr := `
            select top 20
            a.[Id], a.[CreateDate],a.[PublisherId],a.[NickName],a.[FileSize],a.[Followed],
            a.[Readed],a.[FilePath], b.[STitle],a.[CreateDate],a.[Encryptioned],a.[FileName]
            from [hds12204021_db].[dbo].[Web2019_historylist] a, [hds12204021_db].[dbo].[Web2019_roomlist] b
            where a.roomid = ? and a.Deleted = 0 and a.roomid=b.id and a.[CreateDate] < ?
            order by a.CreateDate desc
        `
        log.Println(sqlstr)
        stmt1, err0 := db.Prepare(sqlstr)
上一篇下一篇

猜你喜欢

热点阅读