Note that we are using the "concat" function to join the two columns together.
Now we will use a case structure. You start with "case" then say "when" and then in parenths you give your condition then you use a "then" then you give what you want to happen. Then at the end you put an 'else' and the default that happens if none of the conditions are met. Then you end it with an "end name"
Now see if you can use "union" to completely get rid of the case statement.
We still have two joins we need to talk about. The Natural Join and the Cross Join.
You will probably never use the natural join, but in case you ever see it, here is what it does. Let's say you want to have a list of account_id and cust_id, you say To do this with a natural join: Now for our last topic...Natural Join
select a.account_id, c.cust_id
from account a join customer c
on a.cust_id = c.cust_id;
select a.accoun_id, c.cust_id
from account a natural join customer c;
Cross Join
You have already seen something like this:
select a.account_id, c.cust_id from account a join customer c;
wow. That's alot of rows.
In rare occasions you will need to do this. It is very powerful because it gives you all of the possible combinations of things. Now we will see a case where we need to use this.
Let's say we have accounts opening at different dates. What if the president of the company says that he wants you to create a report. It is going to start with 2004-01-01 then 2004-01-02 etc to 2004-01-31 and keep going until 2004-12-31. So every day in 2004. That's the first column. The second column should show how many accounts opened on that date. We are going to use this function:
date_add('2004-01-01', interval(1) day)
That's it. Feel free to e-mail the teacher (or myself, but I can't garauntee I can help) with any questions.
cmcculloh@gmail.com
mxxue@hotmail.com