Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Mysql query - two tables 1

Status
Not open for further replies.

dle46163

IS-IT--Management
Jul 9, 2004
81
US
Hi All,

I have two tables: Table_a and Table_b. They both have a username field. I want to pull all the usernames in Table_A as long as they are not listed in Table_b. My query for Table_a by itself looks like this:

$query = "SELECT username FROM Table_a
WHERE user_type='{$_SESSION['user_type']}'
ORDER by username";

Can I tell it to exclude usernames which are also found in Table_b, all in the same query? Thanks so much!
 
Hi

You could cut off the PHP code from your posted SQL code. Makes easier to help.
Code:
[b]select[/b]
a.username

[b]from[/b] Table_a a
[b]left join[/b] Table_b b [b]using[/b] (username)

[b]where[/b] a.user_type=[i]'that session value'[/i]
[b]and[/b] b.username [b]is null[/b]

[b]order by[/b] a.username

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top