sql多表查询的问题

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了sql多表查询的问题相关的知识,希望对你有一定的参考价值。

sql多表查询问题
a表(Id,姓名,性别,班级)
例: 001 张三 男 a班
002 李四 男 a班
003 王五 女 b班

b表(ID,成绩)
例: 001 80
002 84
003 90

请问:select a.Id,a.姓名,a.性别,a.班级,b.成绩
from a
where id in
(select id
from b
where id='001')
与 select a.Id,a.姓名,a.性别,a.班级,b.成绩
from a,b
where b.id='001'
查询出来结果之间区别是什么?
怎么将结果导出到EXCEL中去呢?

大概是这个意思,不知道语句有没有写错
如果两表相关联了,查询后的结果一样吗?
把SQL的数据导出至EXCEL表中,以前好像用的时候只要在SQL的查询分析器中打出一条命令就可以了,不用写这么长的代码吧!

如果我要将查询的结果导出EXCEL中,该怎么做呢?

第一种情况查询的目标是a表,where条件里只是从b表筛选条件,所以你那样写的话(b.成绩)是查不出来的。
第二种情况查询的目标是a表和b表的内连接后的结果。
插入excel的方法是:EXEC master..xp_cmdshell 'bcp 库名.dbo.表名 out 导出文件路径 -c -q -S"数据库服务器名" -U"数据库用户名" -P"数据库用户密码"'
如果提示xp_cmdshell不能用的话,需要先在外围应用配置器启动这个功能

如果是查询结果这样写就可以了:EXEC master..xp_cmdshell 'bcp "查询语句" queryout 导出文件路径 -c -q -S"数据库服务器名" -U"数据库用户名" -P"数据库用户密码"'
参考技术A 有很大区别

第一个sql a表和b表相关联

第二个sql a表和b表没有相关联

下面是导出到excel的代码 一个是csv 一个是excel

csv
Sub dB_RsToCSVFile(Rs As ADODB.Recordset, FileName As String, Optional Delimiter As String = ",")
Dim fh As Integer
Dim FileIsOpen As Boolean, s As Variant
Dim t As Integer
Dim Buf As String, TempStr As String
FileIsOpen = False
On Error GoTo Err_Out
fh = FreeFile()
Open FileName For Output As fh
FileIsOpen = True
Buf = ""

For t = 0 To Rs.Fields.Count - 1

If Buf = "" Then
Buf = """" & Rs.Fields(t).Name & """"
Else
Buf = Buf & Delimiter & """" & Rs.Fields(t).Name & """"
End If
Next t

Print #fh, Buf

Do While Not Rs.EOF
Buf = ""

For t = 0 To Rs.Fields.Count - 1

If IsNull(Rs.Fields(t).Value) Then
TempStr = ""
Else
TempStr = Rs.Fields(t).Value
End If

If Buf = "" Then
Buf = """" & TempStr & """"
Else
Buf = Buf & Delimiter & """" & TempStr & """"
End If
Next t
Print #fh, Buf
Rs.MoveNext
Loop
Close fh
Exit Sub
Err_Out:

If FileIsOpen Then
Close fh
End If
MsgBox "当前??: " & Error, vbOKOnly, "??"
End Sub

excel
Option Explicit
Public appdisk As String
Public conn As New ADODB.Connection
Public rs As New ADODB.Recordset
Public db As String
Private Sub Form_Load()
appdisk = Trim(App.Path)
If Right(appdisk, 1) <> "\" Then appdisk = appdisk & "\"
db = appdisk
db = "Provider=Microsoft.Jet.OLEDB.4.0;Persist Security Info=False;Data Source=" & db & "alex.mdb"
conn.CursorLocation = adUseClient
conn.Open db
rs.Open "aaa", conn, adOpenKeyset, adLockPessimistic
End Sub

Private Sub Command1_Click()
Dim lRow As Long
Dim sXLSPath As String
Dim MyExcel As New Excel.Application
Dim MyBook As Excel.Workbook
Dim MySheet As Excel.Worksheet
Screen.MousePointer = 11
sXLSPath = appdisk & "maindata.xls"
Open sXLSPath For Output As #1
Close #1
Set MyExcel = CreateObject("excel.application")
Set MyBook = MyExcel.Workbooks.Open(sXLSPath)
Set MySheet = MyExcel.ActiveSheet
MySheet.Range("A1:O1").Select
With MyExcel.Selection.Interior
.ColorIndex = 15
.Pattern = xlSolid
End With
MySheet.Columns("A:C").NumberFormat = "0_ "
MySheet.Columns(1).ColumnWidth = 5
MySheet.Columns(2).ColumnWidth = 10
MySheet.Columns(3).ColumnWidth = 10
MySheet.Columns(4).ColumnWidth = 10
MySheet.Columns(5).ColumnWidth = 10
MySheet.Columns(6).ColumnWidth = 10
MySheet.Columns(7).ColumnWidth = 10
MySheet.Columns(8).ColumnWidth = 10
MySheet.Columns(9).ColumnWidth = 10
MySheet.Cells(1, 1) = "proid"
MySheet.Cells(1, 2) = "product"
MySheet.Cells(1, 3) = "batchno"
MySheet.Cells(1, 4) = "seqno"
If rs.EOF = True Then
rs.Close: Set rs = Nothing
Screen.MousePointer = 0
Exit Sub
End If
Do While rs.EOF = False
lRow = lRow + 1
MySheet.Cells(lRow + 1, 1) = rs.Fields("seqno")
MySheet.Cells(lRow + 1, 2) = rs.Fields("weight")
MySheet.Cells(lRow + 1, 3) = rs.Fields("unitprc")
MySheet.Cells(lRow + 1, 4) = rs.Fields("account")
rs.MoveNext
Loop
rs.Close: Set rs = Nothing
MyExcel.DisplayAlerts = False
MyBook.SaveAs FileName:=appdisk & "maindata.xls", FileFormat:=xlNormal, Password:="", writerespassword:="", ReadOnlyRecommended:=False, CreateBackup:=False
MyBook.Application.Quit
MyExcel.Application.Quit
Set MySheet = Nothing
Set MyBook = Nothing
Set MyExcel = Nothing
Screen.MousePointer = 0
MsgBox "Successful Established Excell File Maindata.xls", vbOKOnly, "Bawang Electronic Price Scale"
End Sub

SQL查询多表问题

【中文标题】SQL查询多表问题【英文标题】:SQL querying multiple table issue 【发布时间】:2015-08-19 13:31:37 【问题描述】:

我有 3 个表,即子表、员工表和部门表,其中包含一些示例数据。我需要获取:

    孩子的名字 相应员工的名字和姓氏 该员工经理的名字和姓氏。

我想根据上述条件将数据分列在列中。

子表:

+-----------+-------+
| empld_id  | name  |
+-----------+-------+
| 123456789 | Manny |
| 333445555 | Joy   |
| 987654321 | abner |
+-----------+-------+

员工表:

+-------+-------+-----------+-----+-----------+--------+
| fname | lname |    ssn    | sex | superssn  | deptNo |
+-------+-------+-----------+-----+-----------+--------+
| john  | smith | 123456789 | m   | 333445555 |      5 |
+-------+-------+-----------+-----+-----------+--------+

部门表:

+-----------------+--------+------------+
| department_name | deptNo | managerssn |
+-----------------+--------+------------+
| HQ              |      1 | 8888665555 |
| admin           |      4 | 9876543211 |
+-----------------+--------+------------+

我目前有这个查询:

select 
  A.name,
  B.fname, 
  B.lname 
from 
  child A, 
  employee B, 
  department C
where 
  A.empld_id= B.ssn and 
  C.managerssn = B.ssn;

【问题讨论】:

尽量避免逗号分隔的连接! 您是否在表上设置了主键和外键? 在继续操作之前,您肯定应该摆脱ancient join syntax。 【参考方案1】:

你快到了 - 你有经理的 SSN - 现在你需要做的就是将它加入到员工表中以获取他的详细信息。

继续查询的风格:

SELECT c.name, 
       e.fname, e.lname, 
       m.fname AS manager_fname, m.lname AS manager_lname
FROM   child c, employee e, department d, employee m
WHERE  c.empid_id = e.ssn AND d.managerssn = e.ssn AND m.ssn = d.managerssn

但是请注意,隐式连接(在 from 子句中有多个表)已经被弃用了一段时间,建议避免使用它们并改用显式 joins:

SELECT c.name, 
       e.fname, e.lname, 
       m.fname AS manager_fname, m.lname AS manager_lname
FROM   child c
JOIN   employee e ON c.empid_id = e.ssn
JOIN   department d ON d.managerssn = e.ssn
JOIN   employee m ON m.ssn = d.managerssn

【讨论】:

以上是关于sql多表查询的问题的主要内容,如果未能解决你的问题,请参考以下文章

如何进行sql多表查询?

sql多表连接查询问题

多表查询

sql 多表关联查询

SQL语句 - 多表查询使用详细介绍

sql mysql多表如何关联查询