If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > mysql IF or CASE help

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 08-21-06, 15:38
chucklarge chucklarge is offline
Registered User
 
Join Date: Oct 2004
Posts: 2
mysql IF or CASE help

Hello

I am trying to figure out if i can use the mysql control structures in this way.

-Primary-
+-----+----+
| id | name |
+-----+----+
| 1 | Dave |
| 2 | Jason |
| 3 | Jeff |
| 6 | Jeremy |
| 9 | Sam |
+-----+----+

-Shipping-
+-----+----+
| id | name |
+-----+----+
| 1 | Martin|
| 2 | Andrew |
| 3 | Dana |
| 4 | Chris |
| 5 | John |
+-----+----+

-Relation-
+-----+----+------+
| id | type | ps_id |
+-----+----+------+
| 1 | Shipping| 3 |
| 2 | Shipping| 5 |
| 3 | Primary | 9 |
| 4 | Shipping| 1 |
| 5 | Primary | 1 |
+-----+----+------+


Bascially I have two tables with the same fields. I have a 3rd table that that tells me the table and id to select. Right Now, I am doing this with php, but need to find a way to do this with a mysql statement. Can anyone help me with the the control flow statement that will produce the following output ?

Dana
John
Sam
Martin
Dave

Thanks,
Chuck
Reply With Quote
  #2 (permalink)  
Old 08-21-06, 17:51
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Code:
select coalesce(p.name,s.name) as name
  from Relation as r
left outer
  join `Primary` as p
    on p.id = r.ps_id
   and        r.type = 'Primary'
left outer
  join `Shipping` as s
    on s.id = r.ps_id
   and        r.type = 'Shipping'
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On