Ir para conteúdo
Fórum Script Brasil
  • 0

Select direto no Mysql com if else


Leandro H Mendes

Pergunta

presciso fazer uma consulta em duas tabelas porem se o valor não for encontrado na segunda tabela pegar da primeira tabela.

eu presciso pegar todos os produtos que tem estoque > 0 e pegar o valor de custo na tabela mi_itens_nota_fiscal.

se não encontrar o valor na tabela itens nota fiscal pegar do valor do produto

a consulta que eu to ussando é essa :

SELECT p.nome, p.estoque_fiscal ,format(mi.V_comercial,2), format((mi.V_comercial * p.estoque_fiscal),2) as valorFiscal  FROM mi_itens_nfiscal as mi,mi_produtos as p WHERE mi.codigo=p.id and p.estoque_fiscal>0 group by mi.codigo;

porem se ele não encontrar o itens nfiscal ele não pega o produto...

aguardo a ajuda de vocês!!!

não sei se esse é o lugar certo pois sou novo por ak

obrigado

Editado por Leandro H Mendes
Link para o comentário
Compartilhar em outros sites

3 respostass a esta questão

Posts Recomendados

  • 0

CREATE TABLE /*!32312 IF NOT EXISTS*/ `mi_itens_nfiscal` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`tipo_nota` int(10) unsigned DEFAULT '1',

`idNota` int(10) unsigned DEFAULT NULL,

`codigo` char(60) DEFAULT NULL,

`ref` char(60) DEFAULT NULL,

`descricao` char(120) DEFAULT NULL,

`ean` char(14) DEFAULT NULL,

`ncm` char(8) DEFAULT NULL,

`ex_tipi` char(3) DEFAULT NULL,

`cfop` char(4) DEFAULT NULL,

`genero` char(2) DEFAULT NULL,

`u_comercial` char(6) DEFAULT NULL,

`q_comercial` float unsigned DEFAULT '0',

`v_comercial` float unsigned DEFAULT '0',

`u_tributavel` char(6) DEFAULT NULL,

`q_tributavel` float unsigned DEFAULT '0',

`v_tributavel` float unsigned DEFAULT '0',

`ean_tributavel` char(14) DEFAULT NULL,

`total_frete` float unsigned DEFAULT '0',

`total_seguro` float unsigned DEFAULT '0',

`desconto` float unsigned DEFAULT '0',

`valor_total_bruto` float unsigned DEFAULT '0',

`st` char(3) DEFAULT NULL,

`origem` int(1) unsigned DEFAULT NULL,

`modBC` int(1) unsigned DEFAULT NULL,

`vBC` float unsigned DEFAULT '0',

`pRedBC` float unsigned DEFAULT '0',

`pICMS` float unsigned DEFAULT '0',

`vICMS` float unsigned DEFAULT '0',

`modBCST` int(1) unsigned DEFAULT NULL,

`pMVAST` float unsigned DEFAULT '0',

`pRedBCST` float unsigned DEFAULT '0',

`vBCST` float unsigned DEFAULT '0',

`pICMSST` float unsigned DEFAULT '0',

`vICMSST` float unsigned DEFAULT '0',

`pIPI` float unsigned DEFAULT '0',

`vIPI` float unsigned DEFAULT '0',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=14388 DEFAULT CHARSET=latin1;

CREATE TABLE /*!32312 IF NOT EXISTS*/ `mi_produtos` (

`id` int(10) unsigned NOT NULL AUTO_INCREMENT,

`id_fornecedor` int(10) unsigned DEFAULT NULL,

`ref` varchar(50) DEFAULT NULL,

`nome` varchar(100) DEFAULT NULL,

`nome_fiscal` varchar(100) DEFAULT NULL,

`descricao` longtext,

`imagem` varchar(100) DEFAULT NULL,

`codigo` varchar(50) DEFAULT NULL,

`familia` varchar(100) DEFAULT NULL,

`grupo` char(250) DEFAULT NULL,

`cor` varchar(50) DEFAULT NULL,

`tamanho` varchar(50) DEFAULT NULL,

`fabricante` varchar(100) DEFAULT NULL,

`tipo_cod_barra` char(1) DEFAULT 's',

`cod_barra` varchar(50) DEFAULT NULL,

`estoque` int(10) unsigned DEFAULT '10000',

`estoque_minimo` int(10) unsigned DEFAULT '0',

`estoque_maximo` int(10) unsigned DEFAULT '0',

`estoque_seguranca` int(10) unsigned DEFAULT '0',

`estoque_fiscal` int(10) unsigned DEFAULT '0',

`setor` varchar(20) DEFAULT NULL,

`bloco` varchar(20) DEFAULT NULL,

`minimo_venda` int(10) unsigned DEFAULT '0',

`peso_bruto` float unsigned DEFAULT '0',

`peso_liquido` float unsigned DEFAULT '0',

`embalagem` varchar(50) DEFAULT NULL,

`ultima_compra` date DEFAULT NULL,

`ultima_venda` date DEFAULT NULL,

`ds` char(1) DEFAULT NULL,

`da` char(1) DEFAULT NULL,

`dv` char(1) DEFAULT NULL,

`disponivel` char(1) DEFAULT NULL,

`esgotado` char(1) DEFAULT NULL,

`previsao_chegada` date DEFAULT NULL,

`preco_custo` float unsigned DEFAULT '0',

`dolar_compra` float unsigned DEFAULT '0',

`custo_frete` float unsigned DEFAULT '0',

`custo_funcionario` float unsigned DEFAULT '0',

`custo_embalagem` float unsigned DEFAULT '0',

`outras_despesas` float unsigned DEFAULT '0',

`comissao_vendedor` float unsigned DEFAULT '0',

`desconto_permitido` float unsigned DEFAULT '0',

`margem_lucro` float DEFAULT '0',

`preco_venda` float unsigned DEFAULT '0',

`preco_fiscal` float unsigned DEFAULT '0',

`tipo_fiscal` char(1) DEFAULT NULL,

`ultima_alteracao` datetime DEFAULT NULL,

`ultima_alteracao_img` datetime DEFAULT NULL,

`cfop_dentro` int(4) unsigned DEFAULT NULL,

`cfop_fora` int(4) unsigned DEFAULT NULL,

`cfop_st_dentro` int(4) unsigned DEFAULT NULL,

`cfop_st_fora` int(4) unsigned DEFAULT NULL,

`origem` varchar(20) DEFAULT NULL,

`uCom` varchar(6) DEFAULT NULL,

`ncm` char(8) DEFAULT NULL,

`extipi` char(3) DEFAULT NULL,

`ipi` float unsigned DEFAULT '0',

`cEnq` char(3) DEFAULT NULL,

`CNPJProd` char(18) DEFAULT NULL,

`cst` char(3) DEFAULT NULL,

`orig` char(1) DEFAULT NULL,

`modBC` char(1) DEFAULT NULL,

`pICMS` float unsigned DEFAULT '0',

`pRedBC` float unsigned DEFAULT '0',

`modBCST` char(1) DEFAULT NULL,

`pICMSST` float unsigned DEFAULT '0',

`pRedBCST` float unsigned DEFAULT '0',

`pMVAST` float unsigned DEFAULT '0',

PRIMARY KEY (`id`)

) ENGINE=InnoDB AUTO_INCREMENT=5810 DEFAULT CHARSET=latin1;

no caso o que eu preciso é o seguinte

select * form mi_prdutos where estoque_fiscal >0;

select v_comercial from mi_itens_nfiscal as mif where mif.codigo=mi_produtos.id

if v_comercial =0 and v_comercial='' and v_comercial= null

v_comercial := mi_produtos.preco_fiscal

e pra complicar um pouquinho mais tenho que multiplicar esse valor pelo estoque fiscal.

mais tem que ser tudo num sql só

pois vou usar ele no ReportBuilder no Delphi

Link para o comentário
Compartilhar em outros sites

  • 0

nome____________________estoque_fiscal___valor unitario____ subtotal

Carteira Masculina ____________24 ____________ 0.60 ____________14.40

Casaco de Moleton____________5 ____________37.70____________188.50

Valor total 202.90

meu problema esta no valor unitario tenho que pegar ele na tabela nfiscal e se não encontrar pegar na tabela produto

depois multiplicar pelo estoque pra chagar no subtotal e depois fazer a soma dos subtotal

Link para o comentário
Compartilhar em outros sites

Participe da discussão

Você pode postar agora e se registrar depois. Se você já tem uma conta, acesse agora para postar com sua conta.

Visitante
Responder esta pergunta...

×   Você colou conteúdo com formatação.   Remover formatação

  Apenas 75 emoticons são permitidos.

×   Seu link foi incorporado automaticamente.   Exibir como um link em vez disso

×   Seu conteúdo anterior foi restaurado.   Limpar Editor

×   Você não pode colar imagens diretamente. Carregar ou inserir imagens do URL.



  • Estatísticas dos Fóruns

    • Tópicos
      152,1k
    • Posts
      651,9k
×
×
  • Criar Novo...