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 > list country which border both F and D.

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old
Registered User
 
Join Date: Feb 2013
Posts: 10
list country which border both F and D.

I want to list the country which border both ‘F’ and ‘D’.
Suppose we have

INSERT INTO borders VALUES (<Country1>,< Country2>);
INSERT INTO borders VALUES ('B','F');
INSERT INTO borders VALUES ('D','B');
INSERT INTO borders VALUES ('B','NL');
INSERT INTO borders VALUES ('F','D');

I write this code:
Code:
SELECT DISTINCT Country.Name, borders.Country1, borders.Country2 FROM Country
INNER JOIN borders
ON Country.Code = borders.Country1 OR Country.Code = borders.Country2
WHERE borders.Country1 IN ('F','D') AND borders.Country2 IN('D', 'F')
ORDER BY Country.Name;
I expect to see also ‘B’ in result but I just see ‘F’ and ‘D’

Last edited by OmidUOL; 02-17-13 at 08:45.
Reply With Quote
  #2 (permalink)  
Old
Jaded Developer
 
Join Date: Nov 2004
Location: out on a limb
Posts: 12,094
Quote:
Originally Posted by OmidUOL View Post
I want to list the country which border both ‘F’ and ‘D’.
Suppose we have

INSERT INTO borders VALUES (<Country1>,< Country2>);
INSERT INTO borders VALUES ('B','F');
INSERT INTO borders VALUES ('B','D');
INSERT INTO borders VALUES ('B','NL');
INSERT INTO borders VALUES ('F','D');

I write this code:
Code:
SELECT DISTINCT Country.Name, borders.Country1, borders.Country2 FROM Country
INNER JOIN borders
ON Country.Code = borders.Country1 OR Country.Code = borders.Country2
WHERE borders.Country1 IN ('F','D') AND borders.Country2 IN('D', 'F')
ORDER BY Country.Name;
I expect to see also ‘B’ in result but I just see ‘F’ and ‘D
why?
you have said you want rows which have a F or D in country1 AND F or D in country 2, which is what you got.

instead I suspect you want rows which have a F or D in country1 OR F or D in country 2.
__________________
I'd rather be riding my Versys or my Tiger 800 let alone the Norton
Reply With Quote
  #3 (permalink)  
Old
Registered User
 
Join Date: Feb 2013
Posts: 10
I want to list the country which border both ‘F’ and ‘D’.


INSERT INTO Country VALUES (<Code Of Country>, <Name>)
INSERT INTO Country VALUES (D, Germany)
INSERT INTO Country VALUES (F, France)
INSERT INTO Country VALUES (B, BxBx)


INSERT INTO borders VALUES ('B','F');
INSERT INTO borders VALUES ('D','B');
INSERT INTO borders VALUES ('F','D');
INSERT INTO borders VALUES ('W','D');



So how should change the code to see the countries which border both ‘F’ and ‘D’? (in this example B)
please guide me

Last edited by OmidUOL; 02-18-13 at 06:12.
Reply With Quote
  #4 (permalink)  
Old
Registered User
 
Join Date: Feb 2013
Posts: 10
Is there any idea?
please help me

Last edited by OmidUOL; 02-18-13 at 01:48.
Reply With Quote
  #5 (permalink)  
Old
Registered User
 
Join Date: Feb 2013
Posts: 10
Is there any solution for this problem?
Reply With Quote
  #6 (permalink)  
Old
Registered User
 
Join Date: Feb 2008
Location: Japan
Posts: 3,386
How about this?
(Not tested on MySQL.)

Code:
SELECT c.Code_Of_Country
     , c.Name
 FROM  Country AS c
 INNER JOIN
       borders AS b
  ON   c.Code_Of_Country
       IN ( b.Country1 , b.Country2 )
 GROUP BY
       c.Code_Of_Country
     , c.Name
 HAVING
       COUNT(
          CASE
          WHEN CASE c.Code_Of_Country
               WHEN b.Country1 THEN
                    b.Country2
               WHEN b.Country2 THEN
                    b.Country1
               END
               IN ( 'D' , 'F' ) THEN
               0
          END
       ) = 2
;
Reply With Quote
  #7 (permalink)  
Old
Registered User
 
Join Date: Feb 2013
Posts: 10
Thank you very much. it works.
it was very useful
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