|Showing entries 1 to 19|
A very useful helper in your join toolbox can be a delete join. Even though it’s not a special join type but a join used within DELETE statements, it’s still worth mentioning. However, from time to time when I want to make use of delete joins on my own, I somehow managed it to forgot the syntax and have to look it up somewhere. Therefor, here is a description as well as an example.
Take care: A delete join is a very powerful weapon. And with great power comes great responsibility! I recommend to develop the delete join on a development database. At least, make sure you have a working an recent backup before trying to delete things. A delete statement that uses joins makes it easy to shot yourself in the foot. And if you do, it probably blows away your hole leg.
Getting two sets of information from one table in a select statement often leads people to write subselects, but it really doesn't matter that this is the same table twice, we can just give it a new alias and treat it as if it were a different table. This is one of those techniques where, once you've seen it, it's really obvious, but until that point it can be very confusing. I explained this to someone else recently, so I thought I'd capture it here in case it's helpful to anyone else.
Consider that tried-and-tested example: employees and managers. Here's the
staff table from the database (today's imaginary data isn't particularly imaginative, sorry):
mysql> select * from staff; +----+------------+-----------+------------+ | id | first_name | last_name | manager_id |[Read more...]
70x faster joins with AQL
The new GA MySQL Cluster 7.2 Release (7.2.4) just announced by Oracle includes 2 new features which when combined can improve the performance of joins by a factor of 70x (or even higher). The first enhancement is that MySQL Cluster now provides the MySQL Server with better information on the available indexes which allows the MySQL optimizer to automatically produce better query execution plans. Previously it was up to the user to manually provide hints to the optimizer. The second new feature is Adaptive Query Localization which allows the work of[Read more...]
I recently wrote a post about inner and outer joins, and a couple of people asked what the difference is between
In a nutshell, you use
ON for most things, but
USING is a handy shorthand for the situation where the column names are the same.
Consider this example dataset:
mysql> select * from pets; +---------+---------+--------+-----------+ | pets_id | animal | name | owners_id | +---------+---------+--------+-----------+ | 1 | fox | Rusty | 2 | | 2 | cat | Fluffy | 2 | | 3 | cat | Smudge | 3 | | 4 | cat | Toffee | 3 | | 5 | dog |[Read more...]
Someone will probably tell me that this is an elementary-level topic, but I got some good questions regarding joins from my most recent ZCE class students, so I thought I'd put down the examples that I used to explain this to them. Being able to join with confidence is a key skill, because it means that you can refactor and normalise your data, without worrying about how hard something will be to retrieve.
The database structure I'm using here looks like this (the same example that I used when I wrote the Databases chapter for PHP Master):
If you used to write MySQL joins for MySQL versions < 5.0 or upgrade your server from MySQL 4 to MySQL >= 5.0 you maybe run into a problem when you execute the following query:
SELECT * FROM mytable1, mytable2 INNER JOIN mytable3 ON mytable1.mycolumnname = mytable3.mycolumnname WHERE mytable1.id = mytable2.id;
#1054 – Unknown column ‘mytable.mycolumnname’ in ‘on clause’
Even though you made sure that the column exists, the problem persists. It can be a very annoying and time-consuming task to track this kind of error down to it’s cause: MySQL starting from version 5.0 tries to be more compliant to ANSI SQL. The tables are beeing joined in a different order. The solution to this problem is actually very simple. Surround the tables in[Read more...]
Here you find information about writing inner joins with the comma operator. It’s the most basic way to combine (join) two tables. There is an alternative syntax that can be used, because in MySQL you can write inner joins in two different ways. Another popular way is it to use the INNER JOIN command or synonymous keywords like CROSS JOIN and JOIN. Please make sure to read our dedicated documentation for more information when you understand the comma operator syntax.
The following examples are equivalent to the[Read more...]
This tutorial explains how you can use NATURAL JOINs and what a natural join is actually. Included are syntax details and example statements.
Generally speaking, the keyword NATURAL can be treated as a join condition which is added implicitly. If used, it replaces the keywords ON and USING altogether. In MySQL writing natural joins means adding the keyword NATURAL to either an INNER JOIN or an OUTER JOIN. Let’s take a look at how a natural join implies a join condition.
First of all, some natural join syntax examples. As mentioned earlier, a natural join adds a implicit condition to inner and outer join statements:
-- natural inner join SELECT[Read more...]
Here you find information about writing RIGHT JOINs (also referred to as RIGHT OUTER JOINs). This introduction into right joins includes a detailed description, syntax information and right outer join example statements. The Venn diagram on the left represents a result set that a statement with a right join produces. Please refer to the syntax examples below for an example. Links to additional information resources can be found at the end of this article.
First of all, some syntax examples for the impatient:
-- right join with USING-clause SELECT * FROM <leftTable> RIGHT JOIN <rightTable> USING(id)[Read more...]
Here you find information about writing LEFT JOINs (also referred to as LEFT OUTER JOINs). This introduction into left joins includes a description, syntax information and example statements that use left outer joins. The Venn diagram on the left represents a result set that a statement with a left join produces. Please refer to the syntax examples below for an example. Links to additional information resources can be found at the end of this article.
First of all, some syntax examples for the impatient:
-- left join with USING-clause SELECT * FROM <leftTable> LEFT JOIN <rightTable> USING(id)
-- left join with ON-clause SELECT * FROM[Read more...]
Here you find information about OUTER JOINs in MySQL statements. This tutorial is a general introduction. Syntax details and example statements are split into a LEFT OUTER JOIN tutorial and RIGHT OUTER JOIN tutorial because these are specific outer joins.
MySQL (http://www.mysql.com/) supports outer joins. They identify matching rows that are stored in two different tables and add the selected columns to the result set. That’s exactly what an inner join does as well.
The difference between inner and outer join is: An outer join can identify rows without a match in the joined table. When no match was found, MySQL sets the value[Read more...]
This tutorial shows you how to write ANSI-Style inner joins with the INNER JOIN keywords. Included are a general description, some syntax examples and a comparison between inner and cross joins.
Note: In MySQL the join keywords JOIN and CROSS JOIN are synonymous with INNER JOIN. That means: All example statements found in this article work fine when you replace INNER JOIN with JOIN or CROSS JOIN.
Here are syntax examples for the impatient. Basically, ANSI-style join conditions can be specified with two different keywords: USING and ON. Take a look at the following examples:
-- inner join with USING clause SELECT * FROM <firstTable> a INNER JOIN <anotherTable> b USING(<columnName>)[Read more...]
Last month at the Boston MySQL User Group, I went through the meanings of INNER, LEFT/RIGHT OUTER, CROSS, NATURAL joins, how to do a FULL OUTER JOIN in MySQL, and what STRAIGHT_JOIN means. I also explained how to recognize when you want those types of joins, and best practices for the semantics of writing joins and design patterns. Subqueries were explained in this session, and some examples of how to think differently so that you end up writing JOINs instead of subqueries. The slides (slightly different from the slides in the video — due to error correction) can be found at http://technocation.org/files/doc/2010_01MySQLJoins.pdf.
Here’s the video:
Here’s a little something that might trip you up occasionally. Have a look at this test scenario:
USE test; DROP TABLE IF EXISTS a; DROP TABLE IF EXISTS b; DROP TABLE IF EXISTS c; CREATE TABLE a ( a INT ); CREATE TABLE b ( b INT ); CREATE TABLE c ( c INT ); SELECT a.a FROM a LEFT JOIN c ON c.c = a.a; -- Q1 SELECT a.a FROM a, b LEFT JOIN c ON c.c = a.a; -- Q2
Q1 and Q2 will produce the same result, right? Wrong! As of MySQL 5.0.12, per the SQL standard, JOIN has had higher precedence than comma ‘,’.
So, you get the following:
mysql> SELECT a.a FROM a LEFT JOIN c ON c.c = a.a; Empty set (0.00 sec) mysql> SELECT a.a FROM a, b LEFT JOIN c ON c.c = a.a; ERROR 1054 (42S22): Unknown column 'a.a' in 'on clause'
This is because, in earlier versions, MySQL interpreted it as ( ( a, b[Read more...]
Being a serious Perl addict since 20 years, I of course start missing a lot of nice functions for manipulating strings, and the most immediate one is join, so I wrote a C++ IOStream manipulator to join the elements of an arbitrary sequence and output them to an
In this case, since the I/O Manipulator takes arguments, it has to be written as a[Read more...]
By “Good SQL Querying”, I am not referring to “how to make your queries more perfomant.” I am about to go on a mini-rant about how to make readable and self-documenting SQL queries.
One practice that will get me instantly going on a rant is using a comma join. There is NO reason to do the following:
-- uses the sakila sample database SELECT first_name, last_name, address FROM customer,address;
What kind of join did the original author intend? A CROSS JOIN? Or did they really want an INNER JOIN and forget the WHERE clause?
The answer: you do not know for sure; you can only guess. Had the query been
SELECT first_name,last_name,address FROM customer INNER JOIN address;
you would know that the author intended an INNER JOIN; had the query been
SELECT first_name,last_name,address FROM customer CROSS JOIN[Read more...]
|Showing entries 1 to 19|