English 中文(简体)
这2 sql 命令有什么区别?
原标题:What the difference between these 2 sql commands?
  • 时间:2012-05-24 14:49:01
  •  标签:
  • sql
select * from StudySQL.dbo.id_name n
inner join StudySQL.dbo.id_sex s
on n.id=s.id
and s.sex= f 

select * from StudySQL.dbo.id_name n
inner join StudySQL.dbo.id_sex s
on n.id=s.id
where s.sex= f 

结果完全一样。 那么它们之间有什么区别吗?

Add

我做了几次更有趣的尝试

select * from StudySQL.dbo.id_name n

1 | baby
3 | alice

select * from StudySQL.dbo.id_class c

1 | math      
3 | physics   
3 | english   
4 | chinese 


select * from StudySQL.dbo.id_name n
left join StudySQL.dbo.id_class c
on n.name= alice 

name  id    id  class
baby    1   NULL    NULL
alice   3   1           math      
alice   3   3           physics   
alice   3   3           english   
alice   3   4           chinese   


select * from StudySQL.dbo.id_name n
left join StudySQL.dbo.id_class c
on n.name= baby 

name    id  id  class
baby    1   1           math      
baby    1   3           physics   
baby    1   3           english   
baby    1   4           chinese   
alice   3   NULL    NULL


select * from StudySQL.dbo.id_name n
left join StudySQL.dbo.id_class c
on n.name<>  

name    id  id  class
baby   1    1           math      
baby   1    3           physics   
baby   1    3           english   
baby   1    4           chinese   
alice  3    1           math      
alice  3    3           physics   
alice  3    3           english   
alice  3    4           chinese   

So I thinnk it s reasonable to say, the on clause decides which rows should be joined. While the where clause decides which rows should be returned.

如果情况属实,<强势>我认为最好在条款中写明详细的限制,这样需要加入的行就更少。 加入是一个昂贵的操作。

最佳回答

Like Marc Romero said, the ON clause is to specify the relation between tables, the WHERE clause is to specify the filter condition. In this particular query, you ll see the same results, but in others if you don t understand the difference you could get unexpected results. Consider the queries (yours with LEFT JOINS instead of INNER) below:

SELECT *
FROM StudySQL.dbo.id_name n
LEFT JOIN StudySQL.dbo.id_sex s ON n.id=s.id AND s.sex= f 

SELECT *
FROM StudySQL.dbo.id_name n
LEFT JOIN StudySQL.dbo.id_sex s on n.id=s.id
WHERE s.sex= f 

第一张将回复来自InstituteSQL.dbo.id_name的信息和与任何有性别记录相关的SQL.dbo.id_exexex表的信息。 第二张将回复所有姓名,但只有有性别名称的 " f " 将回复来自InstitutSQL.dbo.id_exexemblogination的信息。

问题回答

这是 INNER JOIN ,但是如果使用 OUTER JOIN ,结果会大不相同。使用 LEFT OUTER JOIN 后,第二个查询将隐含成为 INNER JOIN

通过使用 , 上游是指从 s 到 n 的行应连接到 n。 在内部连接中, 负结果将防止 n 和 s 边被省略 。

有关条款是为了明确表格之间的关系,“何处条款”是为了明确过滤条件,也许更是一个概念问题,在这种情况下,它更是一个结果/业绩问题。





相关问题
SQL SubQuery getting particular column

I noticed that there were some threads with similar questions, and I did look through them but did not really get a convincing answer. Here s my question: The subquery below returns a Table with 3 ...

难以执行 REGEXP_SUBSTR

I m 查询Oracle 10g。 我有两张表格(样本数据见下文)。 i m 试图提取一些领域

SQL Query Shortcuts

What are some cool SQL shorthands that you know of? For example, something I learned today is you can specify to group by an index: SELECT col1, col2 FROM table GROUP BY 2 This will group by col2

PHP array callback functions for cleaning output

I have an array of output from a database. I am wondering what the cleanest way to filter the values is example array Array ( [0] => Array ( [title] => title 1 ...

OracleParameter and DBNull.Value

we have a table in an Oracle Database which contains a column with the type Char(3 Byte). Now we use a parameterized sql to select some rows with a DBNull.Value and it doesn t work: OracleCommand ...

Running numbers in SQL

I have a SQL-statement like this: SELECT name FROM users WHERE deleted = 0; How can i create a result set with a running number in the first row? So the result would look like this: 1 Name_1 2 ...

How to get SQL queries for each user where env is production

I’m developing an application dedicated to generate statistical reports, I would like that user after saving their stat report they save sql queries too. To do that I wrote the following module: ...

热门标签