Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

select using if/else??

0 views
Skip to first unread message

cruiserweight

unread,
Jul 22, 2008, 10:46:17 PM7/22/08
to
I have two tables: itineraries and schedules. itineraries has a unique
id and a text description. schedules has a unique id, an itinerary id
as foreign key, and a date for departure. an itinerary may or may not
have schedule information.

i want to select itinerary_id, itinerary_description and, if it
exists, the schedule departure date. but if there is no departure
date, no schedule data, i still need the itinerary info. i have no
idea how to accomplish this. any and all ideas would be massively
appreciated.

Michael Austin

unread,
Jul 22, 2008, 11:19:09 PM7/22/08
to


What have you tried? Look at the left outer join and case statements.
Case is not mandatory - if returning NULL is sufficient.

Given:
mysql> select * from a;
+---+------+------+
| z | a | b |
+---+------+------+
| 1 | a | 1 |
| 2 | a | 2 |
+---+------+------+
2 rows in set (0.01 sec)

mysql> select * from b;
+------+------+
| a | b |
+------+------+
| b | 2 |
| c | 3 |
+------+------+

EXAMPLE:


mysql> select a.a, a.b, case when b.a is null then 'NoData' else b.a end
as c
-> from a a left outer join b b on a.b=b.b where a.b=1;
+------+------+--------+
| a | b | c |
+------+------+--------+
| a | 1 | NoData |
+------+------+--------+

cruiserweight

unread,
Jul 22, 2008, 11:58:31 PM7/22/08
to
On Jul 23, 10:19 am, Michael Austin <maus...@firstdbasource.com>
wrote:

I hadn't tried anything cuz i didn't know where to start. Trying to
explain things as clearly as i could in the original post made me
think LEFT join, so i started googling that. I found this page,
http://www.wellho.net/solutions/mysql-left-joins-to-link-three-or-more-tables.html,
which explained it to me. If fact my above explanation was
oversimplified. I had three tables. Understanding left join was the
key. Thanks for the help.

0 new messages