要加入的sql查询存储在数据集中的表和列名称

时间:2015-11-03 07:34:26

标签: sql vb.net ms-access join dataset

我有3个表,在我无法修改的访问数据库中,使用左连接进行查询,如此

SELECT
  tbl_A.mID,
  tbl_A.data,
  tbl_B.data,
  tbl_C.data
FROM ((tbl_A
  LEFT JOIN tbl_B ON tbl_A.mID = tbl_B.mID)
  LEFT JOIN tbl_C ON tbl_A.mID = tbl_C.mID)
ORDER BY tbl_A.mID

现在我想存储列名,包括左连接中对应表的表名

   Dim i as integer = 0
    For Each column As DataColumn In ds.Tables(0).Columns
      Dim lst As New ListBox
      lst.name = "lst_" & i
      lst.add(new ColumnAndListBox(ds.Tables(0).TableName & "." & column.ColumnName, lst.name)
      i += 1
    next

该列表中的结果应如下所示

tbl_A.mID  lst
tbl_A.data lst
tbl_B.data lst
tbl_C.data lst

我需要完整的名称(表和列)才能动态创建where子句并查询例如mID,因为像这样的查询不起作用

SELECT
      tbl_A.mID,
      tbl_A.data,
      tbl_B.data,
      tbl_C.data
    FROM ((tbl_A
      LEFT JOIN tbl_B ON tbl_A.mID = tbl_B.mID)
      LEFT JOIN tbl_C ON tbl_A.mID = tbl_C.mID)
    WHERE mID = 1
    ORDER BY tbl_A.mID

1 个答案:

答案 0 :(得分:1)

SELECT * from
  (
   SELECT
      tbl_A.mID,
      tbl_A.data adata,
      tbl_B.data bdata,
      tbl_C.data cdata
   FROM ((tbl_A
      LEFT JOIN tbl_B ON tbl_A.mID = tbl_B.mID)
      LEFT JOIN tbl_C ON tbl_A.mID = tbl_C.mID)
   ORDER BY tbl_A.mID
   )     WHERE mID = 1

不起作用,但我认为这会:

datee