【问题标题】:Simplify return data from join SQL简化连接 SQL 的返回数据
【发布时间】:2018-02-03 09:03:03
【问题描述】:

我只是好奇,我如何concat 加入表,这样结果就不会返回很多行?

我想要的是名字dinterest 列中只返回一行001;004;005;003;007,所以在我的网格中不会显示很多行。

这是我的代码

 .CommandText = String.Format("select aa.code, aa.firstname, cc.interest as interest from db.name aa ") _
 '& ("left join db.interest cc on cc.lead = aa.code ") _
 '& ("where convert(date,time) between '" & date1& "' and '" & date2& "' order by aa.code ")

我在这里问是因为我不知道与此相关的关键字

【问题讨论】:

  • 用您正在使用的数据库标记您的问题。
  • 请不要通过将字符串连接在一起来编写这样的查询。而是使用适当的参数化查询。它将使您免受晦涩的语法错误以及(更重要的是)SQL 注入攻击。请参阅bobby-tables.com 以了解您的代码为何易受攻击,并查看一些如何正确安全地执行此操作的示例(它们使用 C#,但您可以轻松地将其在线转换为 VB,或在其他地方查找相同的内容)跨度>
  • @ADyson 感谢您的关注。但现在我不认为这会得到 sql 注入,因为它不是用于键入而不是按钮的文本框。我需要一个关键字来解决这个问题:)
  • @chopperfield 日期变量的生成方式并不重要,作为一般规则,您不应该信任从应用程序返回的任何内容,因为它可能会以某种方式受到干扰。在桌面应用程序中可能比在 Web 应用程序中发生这种情况的可能性要小一些,但您不应该认为它没问题。 Plus 参数将使您免于其他问题,例如意外的语法错误等。

标签: sql sql-server vb.net


【解决方案1】:

您需要在查询中添加GROUP BY 声明:

 .CommandText = String.Format("select aa.code, aa.firstname,COUNT(aa.firstname) as 'FnameCount', cc.interest as interest from db.name aa ") _
 '& ("left join db.interest cc on cc.lead = aa.code ") _
 '& ("where convert(date,time) between '" & date1& "' and '" & date2& "' GROUP BY aa.code, aa.firstname, cc.interest  order by aa.code ")

【讨论】:

    猜你喜欢
    • 2013-11-25
    • 2014-10-08
    • 2023-03-26
    • 1970-01-01
    • 2014-04-18
    • 2021-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多