0

I have 3 tables named person,person1, person2. All the 3 table has 2 columns name and phno. Now my requirement is, if i give any particular phno it has to show either yes or no for the corresponding table.

My sample table is :

Person Table:
Name      phno
abc       1234
def       2345
ghi       3456

and person2 table is

Name      phno
abc       1234
def       2345
ghi              

and person3 table is

Name       phno
abc         
def        2345
ghi        3456

As you can see, the same name can contain phno in one table and not in the other table. The required output is like :

Output 1:
 Phno            person         person2     person3
 1234              yes            yes         no

Output 2:
phno              person         person2     person3
3456                 yes           no             yes

I have tried something like this..

select a.phno,case when a.phno = '1234'then 'Y' else 'N' end as phone_number1,
case when b.phno = '1234' then 'Y' else 'N' end as phone_number2,
case when c.phno = '1234' then 'Y' else 'N' end as phone_number3
from person as a, person2 as b, person3 as c
where a.phno = '1234' and b.phno = '1234' and c.phno = '1234';

can anyone help me to improve this to achieve what I want.Thanks

2 Answers 2

1

MySQL does not support full outer joins, so you will have to use unions:

select phno,
if(count(phno1) = 0, 'No', 'Yes') as person,
if(count(phno2) = 0, 'No', 'Yes') as person2,
if(count(phno3) = 0, 'No', 'Yes') as person3,
from (select phno, phno as phno1, NULL as phno2 , NULL as phno3 from person  
      union
      select phno, NULL as phno1, phno as phno2 , NULL as phno3 from person2   
      union
      select phno, NULL as phno1, NULL as phno2 , phno as phno3 from person3
) sub
group by phno

If you only want to check for a single person, you can use Aditys code slight modifications:

SELECT DISTINCT p.phno AS 'phno',
(CASE WHEN ISNULL(person.phno) THEN 'NO' ELSE 'Yes' END) AS 'person',
(CASE WHEN ISNULL(person2.phno) THEN 'NO' ELSE 'Yes' END) AS 'person2',
(CASE WHEN ISNULL(person3.phno) THEN 'NO' ELSE 'Yes' END ) AS 'person3'
FROM (SELECT 4567 AS phno) p
LEFT JOIN person ON p.phno = person.phno
LEFT JOIN person2 ON p.phno = person2.phno
LEFT JOIN person3 ON p.phno = person3.phno;

It does work, I just tested it myself, feel free to execute my test and confirm:

DROP TABLE IF EXISTS person;
CREATE TABLE person(NAME VARCHAR(255), phno INT);
DROP TABLE IF EXISTS person2;
CREATE TABLE person2(NAME VARCHAR(255), phno INT);
DROP TABLE IF EXISTS person3;
CREATE TABLE person3(NAME VARCHAR(255), phno INT);

INSERT INTO person(NAME, phno)
VALUES ('abc', 1234),('def', 2345),('ghi', 3456),('jkl', NULL);

INSERT INTO person2(NAME, phno)
VALUES ('abc', 1234),('def', 2345),('ghi', NULL),('jkl', 4567);

INSERT INTO person3(NAME, phno)
VALUES ('abc', NULL),('def', 2345),('ghi', 3456),('jkl', 4567);

SELECT DISTINCT p.phno AS 'phno',
(CASE WHEN ISNULL(person.phno) THEN 'NO' ELSE 'Yes' END) AS 'person',
(CASE WHEN ISNULL(person2.phno) THEN 'NO' ELSE 'Yes' END) AS 'person2',
(CASE WHEN ISNULL(person3.phno) THEN 'NO' ELSE 'Yes' END ) AS 'person3'
FROM (SELECT 4567 AS phno) p
LEFT JOIN person ON p.phno = person.phno
LEFT JOIN person2 ON p.phno = person2.phno
LEFT JOIN person3 ON p.phno = person3.phno;

result:

phno  person  person2  person3
4567  NO      Yes      Yes
6
  • One of my friend said it is possible to achieve this output without using union..ie., using only case statements.
    – Karthik
    Commented Nov 14, 2013 at 14:09
  • not with mysql. The problem is, that you do not seem to have a table with all phone numbers. Hence you neet dto do a full outer join with all 3 tables (like Aditya wrote) but mysql does not support this. If you only want to check for one phone number, then that is possible, i will update my answer with an alterative. Commented Nov 14, 2013 at 15:20
  • Did you understand my requirement fully ? I will give any one number as input and the query has to fetch data for that particular number from all the three tables.
    – Karthik
    Commented Nov 15, 2013 at 9:36
  • that's what the second statement is for. Commented Nov 15, 2013 at 10:51
  • No that is not working
    – Karthik
    Commented Nov 15, 2013 at 11:37
0

Try with this one.

select distinct person.phno as 'phno',
(case when isnull(person.phno) then 'NO' else 'Yes' end) as 'person',
(case when isnull(person2.phno) then 'NO' else 'Yes' end) as 'peron2',
(case when isnull(person3.phno) then 'NO' else 'Yes' end ) as 'person3'
from person  
full outer join person2 on person.phno = person2.phno
full outer join person3 on person3.phno = peron2.phno
5
  • 1
    mysql does not support full outer joins Commented Nov 14, 2013 at 13:19
  • @Aditya as drunken_monkey said MySQL does not support full outer joins. Also one of my friend is saying that it can be achieved by using case statement alone.
    – Karthik
    Commented Nov 14, 2013 at 14:13
  • you don't need outer joins. left join all 3 tables to select 1234 as phno then check for nulls
    – JamesRyan
    Commented Nov 14, 2013 at 14:17
  • Nope, does not work. if you do select from person left join person2 ... then you only get users that are in person. If a phone number is not in person, it will not show up and the case does not state that there is a leading table that contains all phone numbers from which we can then left join. Commented Nov 14, 2013 at 15:21
  • oop's. Consider the logic & try.
    – Aditya
    Commented Nov 15, 2013 at 5:22

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.