Getting data from multiple table issue

Thread Solved

Join Date: Nov 2008
Posts: 11
Reputation: phpdude89 is an unknown quantity at this point 
Solved Threads: 0
phpdude89's Avatar
phpdude89 phpdude89 is offline Offline
Newbie Poster

Getting data from multiple table issue

 
0
  #1
Jun 21st, 2009
Hello...

I need to get data from one table while omitting data that exist in the other table. This is the query I am running but I keep on getting duplicates since there are more the one row in the second table.

Table1 - 'countries' with fields 'Country_Code' and 'Country_Name'
Table2 - 'allow' with field 'Country_Code'

  1. SELECT countries.Country_Code, Country_Name FROM countries, allow WHERE countries.Country_Code != allow.Country_Code ORDER BY Country_Name

Since there are multiple rows in table2(allow) I get duplicate results.

Any help would be appreciated.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 191
Reputation: jbisono is an unknown quantity at this point 
Solved Threads: 24
jbisono's Avatar
jbisono jbisono is offline Offline
Junior Poster

Re: Getting data from multiple table issue

 
0
  #2
Jun 22nd, 2009
try this.

SELECT DISTINCT countries.Country_Code, Country_Name FROM countries, allow WHERE countries.Country_Code != allow.Country_Code ORDER BY Country_Name
Regards.
Last edited by jbisono; Jun 22nd, 2009 at 9:14 am.
If your already resolved your issue, flag it as solved.
José Bisonó
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: phpdude89 is an unknown quantity at this point 
Solved Threads: 0
phpdude89's Avatar
phpdude89 phpdude89 is offline Offline
Newbie Poster

Re: Getting data from multiple table issue

 
0
  #3
Jun 22nd, 2009
Just tried that,

It removes the duplicate but, it selects all the records from countries database and does not check allow database for existing records.

Thanks For the Response...
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 191
Reputation: jbisono is an unknown quantity at this point 
Solved Threads: 24
jbisono's Avatar
jbisono jbisono is offline Offline
Junior Poster

Re: Getting data from multiple table issue

 
0
  #4
Jun 22nd, 2009
SELECT DISTINCT countries.Country_Code, Country_Name FROM countries INNER JOIN allow ON countries.Country_Code != allow.Country_Code ORDER BY Country_Name
What about that.
Last edited by jbisono; Jun 22nd, 2009 at 11:24 am.
If your already resolved your issue, flag it as solved.
José Bisonó
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: phpdude89 is an unknown quantity at this point 
Solved Threads: 0
phpdude89's Avatar
phpdude89 phpdude89 is offline Offline
Newbie Poster

Re: Getting data from multiple table issue

 
0
  #5
Jun 22nd, 2009
Still an issue. it still gets records which it should not for != Compare.

I could try to use a different query to check if the Country Code exist in the second table but if I put it in a loop it will do something like 400 queries which is not good for the DB. Really trying to avoid that.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 191
Reputation: jbisono is an unknown quantity at this point 
Solved Threads: 24
jbisono's Avatar
jbisono jbisono is offline Offline
Junior Poster

Re: Getting data from multiple table issue

 
0
  #6
Jun 22nd, 2009
Ok, I think I am a little lost lets put some data into that table
COUNTRIES
USA - UNITED STATES
DR - DOMINICAN REPUBLIC
MX - MEXICO
PR - PUERTO RICO

ALLOW
DR
MX

So what you are saying is that in ALLOW table you can have multiple DR and MX?
If your already resolved your issue, flag it as solved.
José Bisonó
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: phpdude89 is an unknown quantity at this point 
Solved Threads: 0
phpdude89's Avatar
phpdude89 phpdude89 is offline Offline
Newbie Poster

Re: Getting data from multiple table issue

 
0
  #7
Jun 22nd, 2009
That is exactly how my tables are set up. the problem is that I want to get the Country Name along with their Country Code, but I do not want that information for the countries which are in the allow table. in this case DR and MX.

With that table data above, I would run the query to get only these records:
RESULTS
USA - UNITED STATES
PR - PUERTO RICO
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 191
Reputation: jbisono is an unknown quantity at this point 
Solved Threads: 24
jbisono's Avatar
jbisono jbisono is offline Offline
Junior Poster

Re: Getting data from multiple table issue

 
1
  #8
Jun 23rd, 2009
SELECT country_code, countries
FROM countries
WHERE (country_code NOT IN
(SELECT CountryCode
FROM allow))
ok so your statement should be like this.
regards.
Last edited by jbisono; Jun 23rd, 2009 at 9:19 am.
If your already resolved your issue, flag it as solved.
José Bisonó
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 11
Reputation: phpdude89 is an unknown quantity at this point 
Solved Threads: 0
phpdude89's Avatar
phpdude89 phpdude89 is offline Offline
Newbie Poster

Re: Getting data from multiple table issue

 
0
  #9
Jun 23rd, 2009
Thanks A lot.

That SQL Query works perfectly.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 191
Reputation: jbisono is an unknown quantity at this point 
Solved Threads: 24
jbisono's Avatar
jbisono jbisono is offline Offline
Junior Poster

Re: Getting data from multiple table issue

 
0
  #10
Jun 23rd, 2009
Glad to know that, take care.
If your already resolved your issue, flag it as solved.
José Bisonó
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for MySQL
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC