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 > Data Access, Manipulation & Batch Languages > ANSI SQL > Sorting Results From Two Tables

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 10-07-11, 11:30
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Question Sorting Results From Two Tables

I'm trying to get data from a database but I'm not sure how to achieve results pulling from two separate tables in my database:

I'm trying to return results from the 'users' table (user_id, fname, lname) & their (order_id) from the 'orders' table [b]only for 'users' that have placed an order in the 'orders' table. Can someone help me with how this ANSI SQL SELECT statement would appear?

Right now I only know the following:

Code:
SELECT user_id, fname, lname FROM users...
Reply With Quote
  #2 (permalink)  
Old 10-07-11, 11:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
Code:
SELECT users.user_id
     , users.fname
     , users.lname 
     , orders.order_id
  FROM users
INNER
  JOIN orders
    ON orders.user_id = users.user_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 10-07-11, 12:45
CarlosinFL CarlosinFL is offline
Registered User
 
Join Date: Oct 2010
Location: Orlando, FL
Posts: 184
Question

Thanks that worked beautiful!

Code:
mysql> SELECT users.userId, firstname, lastname, orders.orderId FROM users INNER JOIN orders ON orders.userId = users.userId;+--------+-----------+----------+---------+
| userId | firstname | lastname | orderId |
+--------+-----------+----------+---------+
|      1 | John      | Doe      |       1 | 
|      1 | John      | Doe      |       2 | 
|      2 | Jane      | Smith    |       3 | 
|      2 | Jane      | Smith    |       4 | 
|      3 | Sally     | Jones    |       5 | 
|      4 | Jack      | Dole     |       6 | 
|      4 | Jack      | Dole     |       7 | 
|      4 | Jack      | Dole     |       8 | 
|      4 | Jack      | Dole     |       9 | 
|      4 | Jack      | Dole     |      10 | 
|      4 | Jack      | Dole     |      11 | 
|      5 | Sam       | Jackson  |      12 | 
|     17 | Chris     | Gonzalez |      13 | 
+--------+-----------+----------+---------+
13 rows in set (0.00 sec)
I also played with this statement using the DISTINCT & GROUP BY statements.

Code:
mysql> SELECT DISTINCT(users.userId), firstname, lastname FROM users INNER JOIN orders ON orders.userId = users.userId GROUP BY users.userId;
+--------+-----------+----------+
| userId | firstname | lastname |
+--------+-----------+----------+
|      1 | John      | Doe      | 
|      2 | Jane      | Smith    | 
|      3 | Sally     | Jones    | 
|      4 | Jack      | Dole     | 
|      5 | Sam       | Jackson  | 
|     17 | Chris     | Gonzalez | 
+--------+-----------+----------+
6 rows in set (0.00 sec)
Thank you very much for showing me how this works!
Reply With Quote
  #4 (permalink)  
Old 10-07-11, 12:52
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,524
regarding your first query, you should ~not~ remove table qualifiers from the columns in the query i gave you -- best practice is to qualify ~all~ columns in any query which involves more than one table

regarding your second query, DISTINCT is ~not~ a function, so please do not put parentheses around the first column that comes after the DISTINCT keyword

also, if you use GROUP BY, then DISTINCT is redundant
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 10-10-11, 02:38
JarlH JarlH is offline
Registered User
 
Join Date: Dec 2008
Location: At work...
Posts: 68
Quote:
Originally Posted by r937 View Post
regarding your first query, you should ~not~ remove table qualifiers from the columns in the query i gave you -- best practice is to qualify ~all~ columns in any query which involves more than one table

regarding your second query, DISTINCT is ~not~ a function, so please do not put parentheses around the first column that comes after the DISTINCT keyword

also, if you use GROUP BY, then DISTINCT is redundant
And when GROUP BY each column reference in the SELECT list must either identify a grouping column or be the argument of a set function.
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