从没有列名的查询中选择数据

时间:2011-06-15 15:01:08

标签: sql vba select excel-vba excel

我在VBA中使用SQL来填充电子表格,但是当我这样做时,我得到的数据包括列标题。我试图找出去除信息而没有列名。

例如,

id name job
0  Tom  Repair
1  Bob  Tech

而不是我想要的,

0  Tom  Repair
1  Bob  Tech

3 个答案:

答案 0 :(得分:9)

您可以使用--skip-column-names

运行mysql客户端

答案 1 :(得分:2)

我不确定你是否可以没有列标题...注意:这是mysql,不确定你的dbms,但这是我得到的最接近的:

SELECT id as "", name as "", job as "" FROM table

答案 2 :(得分:1)

了解您没有指出MySQL,在我的工作中,我们使用excel中的ADODB引用使用以下(MySQL)查询。我发布它,因为您可以根据自己的需要进行调整:

Sub AdaptMeToYourUse()
  ' Connection variables
      Dim conn As ADODB.Connection
      Dim server_name As String
      Dim database_name As String
      Dim user_id As String
      Dim password As String
      Dim port_number As String

  ' Establish connection to the database
      server_name = "myserverName" ' 127.0.0.1 if running from a locally
      database_name = "myDBname" ' Enter your database name here
      user_id = "LoginName" ' enter your user ID here
      password = "PWforLoginName" ' Enter your password here
      port_number = "1234"

      Set conn = New ADODB.Connection
      conn.Open "DRIVER={MySQL ODBC 3.51 Driver}" _
        & ";SERVER=" & server_name _
        & ";PORT=" & port_number _
        & ";DATABASE=" & database_name _
        & ";UID=" & user_id _
        & ";PWD=" & password _
        & ";OPTION=16427" ' Option 16427 = Convert LongLong to Int

      Set rs = New ADODB.Recordset

      sqlstr = "SELECT  count(" & SomeField & ") FROM " _
             & SomeTable & " WHERE " & SomeField & " = '" & var2Compare2SomeField & "' "
      rs.Open sqlstr, conn, adOpenStatic, adLockReadOnly, adCmdText 'set appropriate options for you

      Worksheets("SomeWB").Range(SomeRNG).CopyFromRecordset rs

End Sub