Unify case when changing fields values I have the following code:

$sql=
    'select *,'.
    'case when fisicas_cpf is not null then null else razao_social end as razao_social,'.
    'case when fisicas_cpf is not null then null else nome_fant end as nome_fant '.
    'from pessoas p join fisicas f join juridicas j '.
    'on p.fisicas_cpf=f.cpf or p.juridicas_cnpj=j.cnpj '.
    'group by p.id'
;


I want to unify the case statements because they verify the same thing.

I want something like this:

'case when fisicas_cpf is not null then null else razao_social,nome_fant end as razao_social,nome_fant '.


Here, @AaronB says that the case just return one value. This applicate to my situation, or there is a way of doing it?
CASE always returns a single scalar value. You could get to a single-test solution by left joining to a subquery returning a single row with the values only if the condition is met.

However, that would be fairly messy SQL Code. In this simple case you are better off just repeating the case statement. Even though this violates the DRY principle, it is in the end easier to read and maintain.



Correction: MySQL currently does not support correlated subqueries in a JOIN. Therefore the messy solution I mentioned above is not even possible in MySQL. 

Just for completeness, here is the SQL Server solution:

SQL Fiddle

MS SQL Server 2008 Schema Setup:

CREATE TABLE dbo.test(id INT, show_ind INT, col1 VARCHAR(10),col2 VARCHAR(10));

INSERT dbo.test VALUES(1,1,'c11','c21');
INSERT dbo.test VALUES(2,0,'c12','c22');
INSERT dbo.test VALUES(3,1,'c13','c23');
INSERT dbo.test VALUES(4,0,'c14','c24');


Query 1:

SELECT T.id,T.show_ind, XX.col1, XX.col2
  FROM dbo.test T
  CROSS APPLY(
    SELECT TOP(1) X.col1,X.col2
    FROM(
      SELECT 1 x,T.col1,T.col2 WHERE T.show_ind = 1
      UNION ALL
      SELECT 2 x,'',''
    )X
    ORDER BY X.x
  )XX


Results:

| ID | SHOW_IND | COL1 | COL2 |
|----|----------|------|------|
|  1 |        1 |  c11 |  c21 |
|  2 |        0 |      |      |
|  3 |        1 |  c13 |  c23 |
|  4 |        0 |      |      |


You probably agree that it is a lot harder to read than the CASE solution:

SELECT T.id,T.show_ind,
  CASE WHEN T.show_ind = 1 THEN T.col1 ELSE '' END AS col1,
  CASE WHEN T.show_ind = 1 THEN T.col2 ELSE '' END AS col2
FROM dbo.test T;


And these two examples do not even contain the JOINs that are part of your query.