Database Management
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesDatabase Management

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
Be the architects of evolution and help create the mobile internet future. It’s your move---enter to win here!
  #1  
Old August 20th, 2003, 10:28 AM
bigdavejonnyt's Avatar
bigdavejonnyt bigdavejonnyt is offline
lord of err; master of umm
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: uk
Posts: 92 bigdavejonnyt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 47 m 40 sec
Reputation Power: 6
Send a message via ICQ to bigdavejonnyt
sql join question

hi,

i'm almost ashamed to ask, i should be able to get this going on my own, but here goes any way...apologies for using the php tags - the fixed width font makes layout easier !!

two tables - products and where orders orders.productId = products.Id

PHP Code:
 table 1 products                  table 2 orders
+--------------------------+        +---------------------------+             
id productName price |        | OrderId productId qty |
+--------------------------+        +---------------------------+
a1 product1    23.45 |        |     002 a1        |   |
+--------------------------+        +---------------------------+
a2 product2    34.56 |        |     002 a2        |   |
+--------------------------+        +---------------------------+
a3 product3    45.67 |        |     002 a3        |   |
+--------------------------+        +---------------------------+
                                    |     
001 a2        |   |
                                    +---------------------------+
                                    |     
001 a3        |   |
                                    +---------------------------+ 

i want to write a query which shows each product in the table once, and any order quantities against each line where orderId ='001' so the output looks like this.
PHP Code:
 query output
+------------------------------------------------------------------------------------+             
products.id products.productName products.price orders.OrderId |  orders.qty |
+------------------------------------------------------------------------------------+
|          
a1 product1             |          23.45 |                |             |
+------------------------------------------------------------------------------------+
|          
a2 product2             |          34.56 |            001 |           |
+------------------------------------------------------------------------------------+
|          
a3 product3             |          45.67 |            001 |           |
+------------------------------------------------------------------------------------+ 


i remain confident that your thoughts will prove invaluable, and thank you in advance for any time you spend on this.
__________________
rgds
bdjt

Reply With Quote
  #2  
Old August 20th, 2003, 08:26 PM
unatratnag unatratnag is offline
Average Intelligence
Dev Shed Novice (500 - 999 posts)
 
Join Date: Apr 2003
Location: Ohio/Chicago
Posts: 678 unatratnag User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 10 m 22 sec
Reputation Power: 6
Send a message via AIM to unatratnag
You're right, you should have gotten this
Code:
select p.id, p.productName, p.price, ISNULL(o.OrderId,''), ISNULL(o.qty,'')
from products as p,orders as o
WHERE p.id *= o.productid
and o.orderid = '001'
order by p.id


If you just do a left outer join it still grabs the = row you want if the item exists in the left part of the join. easy enough ne?

Last edited by unatratnag : August 20th, 2003 at 08:32 PM.

Reply With Quote
  #3  
Old August 21st, 2003, 03:32 AM
bigdavejonnyt's Avatar
bigdavejonnyt bigdavejonnyt is offline
lord of err; master of umm
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Mar 2003
Location: uk
Posts: 92 bigdavejonnyt User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 6 h 47 m 40 sec
Reputation Power: 6
Send a message via ICQ to bigdavejonnyt
unatratnag

thanks for your answer - i double posted elsewhere and got much the same answer, so i know it would have worked.

thanks again.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesDatabase Management > sql join question


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 6 hosted by Hostway