Can I use IF inside FROM in Mysql?

Asked

Viewed 672 times

-1

It would be possible to do IF within the FROM?
I looked but couldn’t find anything about it.

Follow an example of what I want to do:

SELECT usuario.* FROM (
  SELECT
  IF( 
    (SELECT COUNT(*) FROM pessoa) > 0, 
    SELECT  FROM pessoa,
    SELECT * FROM clliente, 
  ) AS usuario
)

NOTE: As the bank architecture was developed in the worst way, I’m having to do some crazy things in SQL.

  • I’m pretty sure I don’t (because I just tested it here), but I’m sure what you need can be done differently. Do you have any real example of what you need to do with it?

2 answers

1

A solution can be to create a view

create or replace view v_pessoa_cliente
as
select cpf , nome from pessoa
union
select cpf,nome from cliente

In the application do

select *
from v_pessoa_cliente
where nome = 'Daniel Dutra'

0

Simplifying like that would no longer solve your case?

SELECT IF(COUNT(p.*) > 0, p.*, c.*) as usuarios_maior_volume
FROM pessoas p, clientes c;

And to catch global:

SELECT * FROM pessoas, clientes;

And to bring it all:

SELECT * FROM pessoas
union all
SELECT * FROM clientes;
  • I was unsuccessful, it shows Mysql version error. version: 5.0.95-log; version_bdb: Sleepycat Software: Berkeley DB 4.1.24: (December 16, 2011)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.