简体   繁体   English

SQL查询涉及多个表

[英]Sql Query involving multiple tables

I have two tables.. 我有两张桌子。

Persons: 人数:

  empid(primary key)
  firstname
  lastname
  email

Details: 细节:

  Did(primary key)
  salary
  designation
  empid

Now I need to SELECT first name and last name of employees whose designation is Manager . 现在,我需要SELECT名称为Manager的员工的名字和姓氏。

I am a beginner, I have been learning SQL from W3school (which is awesome btw), if you can suggest me where should I go after completing w3school that would be just great! 我是一个初学者,我一直在从W3school学习SQL(很棒的顺便说一句),如果您能建议我完成w3school后应该去哪里,那将是很棒的!

It's pretty much as you said it, with a simple join on the column that relates the two tables together (in this case EmpID ): 就像您说的那样,在将两个表关联在一起的列上进行了简单连接(在本例中为EmpID ):

SELECT firstname,
       lastname
FROM   Persons
       INNER JOIN Details
         ON Persons.EmpID = Details.EmpID
WHERE  designation = 'Manager' 

As for best source of knowledge, you can't beat books, MSDN, and StackOverFlow if you ask me. 至于最好的知识来源,如果您问我,您将无法击败书籍,MSDN和StackOverFlow。 There's a good few blogs around too - but they tend to be for more advanced topics. 也有很多博客-但是它们往往是针对更高级的主题的。 The writers tend to hang around SO anyway! 无论如何,作家们总是闲逛!

The query you're looking for: 您要查询的查询:

select t1.firstname, t1.lastname
from person t1 inner join
details t2 on t1.empid = t2.empid
where t2.designation = 'Manager'

As for learning Sql on the internet, i dont really know a great place for tutorials, but since you're using sql server 2008 i suggest you often consult the MSDN . 至于在互联网上学习Sql,我真的不知道教程的好地方,但是由于您使用的是sql server 2008,所以我建议您经常咨询MSDN If you're motivated you can find very indepth information there. 如果您有动力,可以在那里找到非常深入的信息。

I don't know specifically if this works for SQL Server 2008, but this should be rather standard SQL: 我不知道这是否适用于SQL Server 2008,但这应该是标准的SQL:

SELECT firstname, lastname
FROM Persons
INNER JOIN Details ON Persons.empid = Details.empid
WHERE Details.designation = 'Manager'
Select p.fname, p.lname
from persons p, details d
where d.designation="Manager" and p.empid=d.empid

Using the single character thing, p. 使用单个字符的东西, p. and d. d. , is a way of just doing shorthand and save typing. ,是一种简化操作并保存输入内容的方法。

And as for SQL source, I am a big fan of tizag. 至于SQL源,我非常喜欢tizag。 Usually one of my main goto's for PHP, SQL, and anything, really. 通常,我的主要goto之一实际上是用于PHP,SQL等。 W3 first, Stack Overflow second, and tizag third. W3首先,堆栈溢出第二,然后tizag第三。 I always find the answer that way. 我总是以这种方式找到答案。

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

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