Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
swdev:howto:perform_sql_queries [2009/02/03 11:07] smayr |
swdev:howto:perform_sql_queries [2015/06/05 10:30] (current) smayr |
||
---|---|---|---|
Line 1: | Line 1: | ||
== Performing SQL Queries == | == Performing SQL Queries == | ||
- | == Sample Queries | + | == SQL JOINs == |
+ | {{: | ||
+ | |||
+ | * INNER JOIN: returns rows when there is a match in both tables. <code sql> | ||
+ | FROM `t1` | ||
+ | INNER JOIN `t2` ON `t1`.`id` = `t2`.`id`;</ | ||
+ | * LEFT JOIN: returns all rows from the left table, even if there are no matches in the right table. <code sql> | ||
+ | FROM `t1` | ||
+ | LEFT OUTER JOIN `t2` ON `t1`.`id` = `t2`.`id`;</ | ||
+ | * RIGHT JOIN: returns all rows from the right table, even if there are no matches in the left table. <code sql> | ||
+ | FROM `t1` | ||
+ | RIGHT OUTER JOIN `t2` ON `t1`.`id` = `t2`.`id`;</ | ||
+ | * FULL JOIN: returns rows when there is a match in one of the tables. <code sql>/* MySQL requires a UNION */ | ||
+ | (SELECT ... FROM tbl1 LEFT JOIN tbl2 ...) | ||
+ | UNION ALL /* to see duplicates (instead of UNION) */ | ||
+ | (SELECT ... FROM tbl1 RIGHT JOIN tbl2 ... | ||
+ | WHERE tbl1.col IS NULL /* To display duplicates in MySQL */ | ||
+ | * SELF JOIN: is used to join a table to itself as if the table were two tables, temporarily renaming at least one table in the SQL statement. | ||
+ | * CARTESIAN JOIN: returns the Cartesian product of the sets of records from the two or more joined tables. | ||
+ | |||
+ | Source: | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * [[http:// | ||
+ | * Image: [[http:// | ||
+ | == Sample Queries == | ||
=== Find Distinct Records (keeping the higher value) === | === Find Distinct Records (keeping the higher value) === |