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 > need help with SELECT logic

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-13-06, 14:36
trefrog trefrog is offline
Registered User
 
Join Date: Jan 2006
Posts: 7
Question need help with SELECT logic

I have 2 tables and I need to select conditionally from both, I don't know why this isn't working. The purpose of this is (obviously?) to enforce unique usernames while preserving data about 'dead' users.
Here is my little test database:
Code:
mysql> select userID, username from user_info;
+----------+----------+
| userID   | username |
+----------+----------+
| 00000001 | nwarden  |
| 00000002 | ahajas   |
| 00000004 | nwarden  |
+----------+----------+

mysql> select userID, SubStatus from user_stats;
+----------+------------+
| userID   | SubStatus  |
+----------+------------+
| 00000001 | Active     |
| 00000002 | Terminated |
| 00000004 | Terminated |
+----------+------------+
This is the query that I'm using:
Code:
mysql> SELECT i.userID, i.username FROM user_info AS i, user_stats AS s WHERE i.
username='nwarden' AND s.SubStatus<>'Terminated';
+----------+----------+
| userID   | username |
+----------+----------+
| 00000001 | nwarden  |
| 00000004 | nwarden  |
+----------+----------+
But this is not what I expected. This is what I want:
Code:
+----------+----------+
| userID   | username |
+----------+----------+
| 00000001 | nwarden  |
+----------+----------+
What am I doing wrong?
Reply With Quote
  #2 (permalink)  
Old 02-13-06, 15:25
RandyRiegel RandyRiegel is offline
Registered User
 
Join Date: Sep 2003
Posts: 22
I get the desired results by using a join like this:

Code:
select user_info.userid, username from user_info
    join user_stats on user_info.userid = user_stats.userid 
    where username = 'nwarden' and 
        user_stats.substatus <> 'Terminated';
Reply With Quote
  #3 (permalink)  
Old 02-13-06, 17:19
trefrog trefrog is offline
Registered User
 
Join Date: Jan 2006
Posts: 7
Voila! Merci Beaucoup!
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