不想获取单列(名称)

发布于 2025-01-11 13:31:50 字数 1202 浏览 0 评论 0原文

def fetchProposalByStudio(studioId: Int): List[ProposalDetails] = {
ConnectionPoolManager.getDB(config = appConfig).localTx { implicit session: DBSession =>

  logger.info("Querying proposal table to fetch all the proposals")
  SQL("""SELECT [except name] p.id, id, proposal_title, challenge, possible_solution, explanation,
        |  submission_date, status, submitted_by, remark
        | FROM proposal p inner join knolder k on k.id = p.knolder_id
        | where k.studio_id =? order by p.id desc""".stripMargin)
    .bind(studioId)
    .map(rs =>
      ProposalDetails(
        rs.int("id"),
        rs.int("id"),
        rs.string("proposal_title"),
        rs.string("challenge"),
        rs.string("possible_solution"),
        rs.string("explanation"),
        rs.string("submission_date"),
        Some(ProposalStatus.withName(rs.string("status"))),
        rs.string("submitted_by"),
        rs.string("remark"),
        **rs.string("name")**
      )
    )
    .list().apply()
}

我不想在查询中获取此列名称,但如果不将其包含在查询中,我会由于使用案例类而收到此错误

13:28:24.446 [default-akka.actor.default-dispatcher-8] 信息 cklbProposalImpl - 获取提案时出现问题。异常消息:错误:“[”处或附近存在语法错误 位置:8

def fetchProposalByStudio(studioId: Int): List[ProposalDetails] = {
ConnectionPoolManager.getDB(config = appConfig).localTx { implicit session: DBSession =>

  logger.info("Querying proposal table to fetch all the proposals")
  SQL("""SELECT [except name] p.id, id, proposal_title, challenge, possible_solution, explanation,
        |  submission_date, status, submitted_by, remark
        | FROM proposal p inner join knolder k on k.id = p.knolder_id
        | where k.studio_id =? order by p.id desc""".stripMargin)
    .bind(studioId)
    .map(rs =>
      ProposalDetails(
        rs.int("id"),
        rs.int("id"),
        rs.string("proposal_title"),
        rs.string("challenge"),
        rs.string("possible_solution"),
        rs.string("explanation"),
        rs.string("submission_date"),
        Some(ProposalStatus.withName(rs.string("status"))),
        rs.string("submitted_by"),
        rs.string("remark"),
        **rs.string("name")**
      )
    )
    .list().apply()
}

}

I don't want to fetch this column name in my query but without involving this in the query i am getting this error due to using case class.

13:28:24.446 [default-akka.actor.default-dispatcher-8] INFO c.k.l.b.ProposalImpl - Something went wrong while fetching the proposals. Exception message: ERROR: syntax error at or near "["
Position: 8

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

蝶舞 2025-01-18 13:31:50

有点语法问题的味道...
也许:

SELECT [except name]  -- should be 
SELECT `except name` -- in mysql

Smells of a syntax problem...
Perhaps:

SELECT [except name]  -- should be 
SELECT `except name` -- in mysql
情独悲 2025-01-18 13:31:50

如果您不希望 SQL 结果集中出现特定列,则只需不在 SELECT 中提及它即可。

没有 SELECT * EXCEPT FirstName FROM person 的概念 - 如果 Person 有 FirstName、LastName、Age、Address 并且您不需要 FirstName,则无需输入它在选择列表中:

SELECT LastName, Age, Address FROM Person

   ^^^^^^^
  no FirstName mentioned here

提及您想要的每一列,不要提及任何您不想要的列。


如果投诉是“但有 527 列,我想要除一列之外的所有列” - 您可以执行以下操作:

SELECT CONCAT(column_name, ',') FROM information_schema.columns WHERE table_name = 'Person' and column_name <> 'FirstName'

生成如下结果集:

LastName,
Age,
Address,
... 523 other columns

然后您可以复制该结果集并将其粘贴到您的代码中,并且它已经带有逗号最后..

如果您希望所有列都在一行上,请使用 GROUP_CONCAT 或使用合适的文本编辑器将 \r\n 替换为空。如果您想用反引号将列名称括起来,请将其放入 CONCAT 中。这里的最终要点是您是软件开发人员:您可以编写编写代码的代码,然后您可以复制输出,这是有效的代码,并将其粘贴到其他地方的其他代码中

If you don't want a particular column in an SQL resultset, you simply don't mention it in the SELECT.

There is no notion of SELECT * EXCEPT FirstName FROM person - if Person has FirstName, LastName, Age, Address and you don't want FirstName, you don't put it in the select list:

SELECT LastName, Age, Address FROM Person

   ^^^^^^^
  no FirstName mentioned here

Mention every column you do want, do not mention any column you don't want.


If the complaint is "but there are 527 columns and I want all except one" - you can do something like:

SELECT CONCAT(column_name, ',') FROM information_schema.columns WHERE table_name = 'Person' and column_name <> 'FirstName'

which produces a resultset like:

LastName,
Age,
Address,
... 523 other columns

And you can then copy that resultset and paste it into your code, and it already has commas on the end..

If you want the columns all on one line, use GROUP_CONCAT or use a decent text editor to replace \r\n with nothing. If you want to surround the column name in backticks, put it into the CONCAT.. The ultimate point here is that you're a software developer: you can write code that writes code, then you can copy the output, which is valid code, and paste it into some other code somewhere else

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文