简体   繁体   English

ms中的sql语法。 访问和vb.net

[英]sql syntax in ms. access and vb.net

i want ask you about sql syntax in microsoft access. 我想问你关于Microsoft Access中的sql语法。

what i want is : 我想要的是:

kdploting kdsubject nmsubject nmlecture
kp001     km001        xx          a
kp001     km002        yy          b
kp001     km003        zz          c

mysql syntax is : mysql的语法是:

select distinct(d.kdploting), c.kdsubject, a.nmsubject, b.nmlecture 
from subject a, mstlecture b, 
transplotingdetail c, transplotingheader d 
where d.kdploting = 'kp001' 
and d.kdploting = c.kdploting 
and c.kdsubject = a.kdsubject 
and d.kdlecture = b.kdlecture;

result : 结果:

kdploting kdsubject nmsubject nmlecture
kp001     km001        xx          a
kp001     km001        xx          b
kp001     km001        xx          c
kp001     km002        yy          a
kp001     km002        yy          b
kp001     km002        yy          c
kp001     km002        zz          a
kp001     km002        zz          b
kp001     km002        zz          c

i don't want the result like this, so can somebody explain me why it can happend? 我不希望这样的结果,所以有人可以向我解释为什么会发生吗? and whats the correct sql query? 什么是正确的SQL查询?

thanks guys! 多谢你们!

note : i really need this query for my vb.net program, thanks guys! 注意:我的vb.net程序确实需要此查询,谢谢!

here my table : 这是我的桌子:

 subject 
    kdsubject nmsubject
    km001     xx
    km002     yy
    km003     zz

 mstlecture 
    kdlecture nmlecture
    kd001     a
    kd002     b
    kd003     c

 Transplotingdetail 
    kdploting kdmatakuliah
    kp001     km001
    kp001     km002
    kp001     km003
    kp002     km001
    kp002     km002
    kp002     km003
    ...

 transploting header 
    kdploting kdlecture
    kp001     kd001
    kp001     kd002
    kp001     kd003
    kp002     kd001
    kp002     kd002
    kp002     kd003    
    ...

What i want is like this 我想要的就是这样

kdploting kdsubject nmsubject nmlecture
kp001     km001        xx          a
kp001     km002        yy          b
kp001     km003        zz          c

At convenience of usage, I have shortened/changed the table/subject names. 为了方便使用,我缩短/更改了表/主题名称。 But using the same data as your sample. 但是使用与样本相同的数据。 The best part of SQL Server featuers are Row_Number() :) Since the query is being done in SQL Server I made use of it. SQL Server特性的最好部分是Row_Number() :)由于查询是在SQL Server中完成的,因此我使用了它。 SQL Server syntax are more aligned with MS ACCESS. SQL Server语法与MS ACCESS更加一致。 So the most important part of the query is to make sure there's a ranking/numbering on the inner records based on distinct subjectid and lectureid . 所以查询的最重要的部分是要确保有基于不同内记录的排序/编号subjectidlectureid Please take a look at the 请看看

* SQLFIDDLE Demo * SQLFIDDLE演示

Query: 查询:

select y.did, x.lid, x.lname,
y.sid, y.sname from
(select ROW_NUMBER() OVER (ORDER BY a.lid) AS Row,
 a.lid, a.lname, b.did
from mstl a
left join (select distinct lid, did from 
           th) as b
on b.lid = a.lid
where b.did = 'kp001') as x
join
(select ROW_NUMBER() OVER (ORDER BY c.sid) AS Row,
 c.sid, c.sname, d.did
from subject c
left join (select distinct sid, did from 
           td) as d
on d.sid = c.sid
where d.did = 'kp001') as y
on x.row = y.row
;

Results: 结果:

DID     LID     LNAME   SID     SNAME
kp001   kd001   a       km001   xx
kp001   kd002   b       km002   yy
kp001   kd003   c       km003   zz

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM