473,394 Members | 1,865 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

How to return the rows that doesn't match two tables?

4
Hi everyone!

I am actually coding a php script and I want to return all the values that doesn't have a relation between two tables.

Basically I have two tables, one is called users, the other one is called agents, the structure of the users table is the following:
ID (auto_increment) | username (email address) | password

the structure of the agents table is the following
ID (auto_increment) | EmailAddress | city | state | country

with
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM dbname.users AS users, dbname.agents AS agents WHERE agents.EmailAddress = users.username
it will return me all the results that matches the two tables. So this works.

But my goal is to display the results that doesn't match, so I tried to do:
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM dbname.users AS users, dbname.agents AS agents WHERE agents.EmailAddress != users.username
but it doesn't display me the expected results.

There is something wrong in my query, but where?

thanks in advance
Nov 25 '07 #1
3 1894
amitpatel66
2,367 Expert 2GB
Hi everyone!

I am actually coding a php script and I want to return all the values that doesn't have a relation between two tables.

Basically I have two tables, one is called users, the other one is called agents, the structure of the users table is the following:
ID (auto_increment) | username (email address) | password

the structure of the agents table is the following
ID (auto_increment) | EmailAddress | city | state | country

with "SELECT * FROM dbname.users AS users, dbname.agents AS agents WHERE agents.EmailAddress = users.username"

it will return me all the results that matches the two tables. So this works.

But my goal is to display the results that doesn't match, so I tried to do:
"SELECT * FROM dbname.users AS users, dbname.agents AS agents WHERE agents.EmailAddress != users.username" but it doesn't display me the expected results.

There is something wrong in my query, but where?

thanks in advance
Try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM agents WHERE emailaddress NOT IN (SELECT username from users)
  2. UNION
  3. SELECT * from users WHERE username NOT IN (SELECT emailaddress FROM agents)
  4.  
Note: The no of columns and its datatypes in both the tables should be same to be used in UNION
Nov 25 '07 #2
mkjame
4
Try this:

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM agents WHERE emailaddress NOT IN (SELECT username from users)
  2. UNION
  3. SELECT * from users WHERE username NOT IN (SELECT emailaddress FROM agents)
  4.  
Note: The no of columns and its datatypes in both the tables should be same to be used in UNION

Thank you for the reply. See my problem is that in one table (dbname.agents) I have 72 rows and in the other table (dbname.users) I have 85 rows. I want to eliminate "automatically" the 13 rows difference and I thought the way to do it is to compare the existing email address in dbname.agents with dbname.users by using a common value which is the email.

Any ideas?

Thank you in advance for your help!!
Nov 25 '07 #3
mwasif
802 Expert 512MB
This will give you those records which are present in users but not in agents.
Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM dbname.users AS users 
  2. LEFT JOIN dbname.agents AS agents ON users.username = agents.EmailAddress
  3. WHERE agents.EmailAddress IS NULL
Nov 25 '07 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Bruce A. Julseth | last post by:
I know the following $sql will fail since there is no Customer='Smith'. I want to determine how to test a failure of mysql_query. I thought mysql_query returned false if a query failed. The test...
2
by: Briniken | last post by:
How can a SQL statement be written to return a specified range of rows? For example: -- tblContact -- ( -- SSN char(9), -- FirstName varchar(50), -- LastName varchar(50) -- ) -- This...
44
by: Mariusz Jedrzejewski | last post by:
Hi, I'll be very grateful if somebody can explain me why my Opera 7.23 (runing under linux) doesn't show me inner tables. Using below code I can see only "inner table 1". There is no problem with...
21
by: Boris Popov | last post by:
Hello pgsql-general, I'm trying to implement a table with rows that are automatically deleted when the session that inserted them disconnects, sort of like our own alternative to...
3
by: Alpha | last post by:
Hi, I have a window C# application. I update the dataset when user finished entering in a text box. But when I want to update the database when OK is pressed, the...
2
by: me | last post by:
I have written a query that joins four or five tables. One table has 30,000 rows. Another table has only 200. I want to only return the 200 or so rows in the smaller table and columns from the...
5
by: samoore33 | last post by:
I use the code below to search through a DataSet: Dim t As DataTable t = result.Tables("State") Dim strExpr As String strExpr = "id = '" & theState.ToString() & "'" Dim foundRows() As DataRow...
0
by: Len H | last post by:
My left table has 700 rows of member data. My right table has 200 rows of roles the members might perform. Both tables have the directory_id. I need to see 700 rows with directory_id, last_name,...
2
by: =?Utf-8?B?QmV0bw==?= | last post by:
I am developing a ASP.NET application, with C# code-behind classes; with SQL Server 2000. All database access is made through stored procedures. I can insert, select and update in all tables...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.