文档详情

JOIN,INSERT句法.docx

发布:2017-08-05约5.28千字共6页下载文档
文本预览下载声明
JOIN句法MySQL支持下列用于SELECT语句的JOIN句法:table_reference, table_referencetable_reference [CROSS] JOIN table_referencetable_reference INNER JOIN table_referencetable_reference STRAIGHT_JOIN table_referencetable_reference LEFT [OUTER] JOIN table_reference ON conditional_exprtable_reference LEFT [OUTER] JOIN table_reference USING (column_list)table_reference NATURAL LEFT [OUTER] JOIN table_reference{ ojtable_reference LEFT OUTER JOIN table_reference ON conditional_expr }上述最后的LEFT OUTER JOIN的句法只是为了与ODBC兼容而存在的。一个表可以是使用aliasedtbl_name AS alias_name或tbl_namealias_name的起的别名。mysql select t1.name, t2.salary from employee AS t1, info AS t2 where t1.name = t2.name;INNER JOIN和,(逗号)在语义上是等价的,都是进行一个在使用的表之间的全联结。通常,你指定表应该如何用WHERE条件联结起来。ON条件是可以用在一个WHERE子句形式的任何条件。如果在一个LEFT JOIN中没有右表的匹配记录,一个所有列设置为NULL的行被用于右表。你可以使用这个事实指出表中在另一个表中没有对应记录的记录:mysql select table1.* from table1 LEFT JOIN table2 ON table1.id=table2.id where table2.id is NULL;这个例子找出在table1中所有的行,其id值在table2中不存在(即,所有table1中的在table2中没有对应行的行)。当然这假定table2.id被声明为NOT NULL。USING (column_list)子句命名一系列必须存在于两个表中的列。例如一个USING子句:A LEFT JOIN B USING (C1,C2,C3,...)被定义成在语义上等同一个这样的ON表达式:A.C1=B.C1 AND A.C2=B.C2 AND A.C3=B.C3,...2个表的NATURAL LEFT JOIN被定义为在语义上等同于一个有USING子句命名在两表中存在的所有列的一个LEFT JOIN。STRAIGHT_JOIN等同于JOIN,除了左表在右表之前被读入,这能用于这些情况,联结优化器将表的顺序放错了。一些例子:mysql select * from table1,table2 where table1.id=table2.id;mysql select * from table1 LEFT JOIN table2 ON table1.id=table2.id;mysql select * from table1 LEFT JOIN table2 USING (id);mysql select * from table1 LEFT JOIN table2 ON table1.id=table2.id LEFT JOIN table3 ON table2.id=table3.id;见10.5.4 MySQL怎样优化LEFT JOIN。INSERT句法 INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO] tbl_name [(col_name,...)] VALUES (expression,...),(...),...或 INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO] tbl_name [(col_name,...)]SELECT ...或 INSERT [LOW_PRIORITY | DELAYED] [IGNORE] [INTO] tbl_name SET col_name=expression, col_name=expression, ...INSERT把新行插入
显示全部
相似文档