Ir para conteúdo

POWERED BY:

Arquivado

Este tópico foi arquivado e está fechado para novas respostas.

cleiton novais

select no mesmo campo

Recommended Posts

Bom dia Galera,

Tenho a seguinte duvida se alguem conseguir me ajudar agradeço.

Ex: tenho 3 tabelas

select * from instituicoes

2 UNIDADE BASICA DE SAÚDE NANCI ABRANCHES

3 CENTRO DE SOLIDARIEDADE AO TRABALHADOR

select * from instituicoes_servicos

2 73 NULL 1999-12-16 09:14:36.000 USU_SINARC SIM

2 103 NULL 1999-12-16 09:19:19.000 USU_SINARC SIM

2 120 NULL 1999-12-16 09:16:33.000 USU_SINARC SIM

2 164 NULL 2004-01-27 10:09:29.000 AFREIRE SIM

2 171 NULL 2004-01-27 10:09:31.000 AFREIRE SIM

2 189 NULL 1999-12-16 09:13:08.000 USU_SINARC SIM

2 209 NULL 1999-12-16 09:18:49.000 USU_SINARC SIM

select * from sevicos

73 CLÍNICA MÉDICA 2002-04-01 09:57:10.000 APARECIDA NULL SIM

103 CURATIVOS 1999-12-06 13:19:40.000 USU_SINARC NULL SIM

120 DIABETES 1999-12-06 13:24:31.000 USU_SINARC NULL SIM

 

Percebam que tenho uma tabela associativa instituicoes_servicos,

Duvida: quando faço este select

Select

INSTITUICOES.CD_INST,

INSTITUICOES.FK_CD_MUNC,

INSTITUICOES.FK_CD_BARR,

SERVICOS.CD_SERV

From

INSTITUICOES_SERVICOS,

INSTITUICOES,

SERVICOS

where

INSTITUICOES_SERVICOS.FK_CD_INST = INSTITUICOES.CD_INST and

SERVICOS.CD_SERV = INSTITUICOES_SERVICOS.FK_CD_SERV and

cd_serv = 73

Funciona nomal

porém quando coloco

 

Select

INSTITUICOES.CD_INST,

INSTITUICOES.FK_CD_MUNC,

INSTITUICOES.FK_CD_BARR,

SERVICOS.CD_SERV

From

INSTITUICOES_SERVICOS,

INSTITUICOES,

SERVICOS

where

INSTITUICOES_SERVICOS.FK_CD_INST = INSTITUICOES.CD_INST and

SERVICOS.CD_SERV = INSTITUICOES_SERVICOS.FK_CD_SERV and

cd_serv = 73 and cd_serv = 103

vem tudo vazio, pois quero fazer este condição no mesmo campo, por que os dois esta vinculado na mesma instituição como faço para fazer este and

Compartilhar este post


Link para o post
Compartilhar em outros sites

cd_serv nõ pode ser 73 e 103 ao mesmo tempo.

 

Select
INSTITUICOES.CD_INST,
INSTITUICOES.FK_CD_MUNC,
INSTITUICOES.FK_CD_BARR,
SERVICOS.CD_SERV
From
INSTITUICOES_SERVICOS,
INSTITUICOES,
SERVICOS
where
INSTITUICOES_SERVICOS.FK_CD_INST = INSTITUICOES.CD_INST and
SERVICOS.CD_SERV = INSTITUICOES_SERVICOS.FK_CD_SERV and
(cd_serv = 73 or cd_serv = 103)

Compartilhar este post


Link para o post
Compartilhar em outros sites

cd_serv nõ pode ser 73 e 103 ao mesmo tempo.

 

Select
INSTITUICOES.CD_INST,
INSTITUICOES.FK_CD_MUNC,
INSTITUICOES.FK_CD_BARR,
SERVICOS.CD_SERV
From
INSTITUICOES_SERVICOS,
INSTITUICOES,
SERVICOS
where
INSTITUICOES_SERVICOS.FK_CD_INST = INSTITUICOES.CD_INST and
SERVICOS.CD_SERV = INSTITUICOES_SERVICOS.FK_CD_SERV and
(cd_serv = 73 or cd_serv = 103)

 

 

Então mas como que eu faço para fazer este and, por que eu tenho 1 instituição ligada a varios sevicos

Ex:

cod_inst = 2 cod_serv = 73

cod_inst = 2 cod_serv = 103

cod_inst = 2 cod_serv = 104

cod_inst = 2 cod_serv = 81

cod_inst = 2 cod_serv = 200

como eu faço para informar que cod_serv =200 and cod_serv = 73 pertence a instituição cod_inst = 2

em forma de select

Compartilhar este post


Link para o post
Compartilhar em outros sites

IN

 

cod_serv IN (73,103,104,81,200)

Então já tinha ate usado o in porém o IN faz o seguinte me traz todas instituicoes que for 73, 103, quero as instituicoes que tiverem 73 e 103.

Ex:

cod_inst = 2 cod_serv = 73

cod_inst = 2 cod_serv = 103

cod_inst = 2 cod_serv = 105

cod_inst = 3 cod_serv = 103

O IN vai trazer ate a instituição cod_inst 103.

 

Na verdade eu quero

cod_inst = 2 cod_serv = 73

cod_inst = 2 cod_serv = 103

cod_inst = 2 cod_serv = 105

cod_inst = 3 cod_serv = 103

se o usuario selecionar o cod_serv = 103 e cod_serv = 73 so venha a instituição cod_inst=2 pois cod_serv = 103 e cod_serv = 73 do tem o cod_inst = 2!

 

O IN me traz todos os valores

 

 

 

 

 

Select

INSTITUICOES.CD_INST,

INSTITUICOES.FK_CD_MUNC,

INSTITUICOES.FK_CD_BARR,

SERVICOS.CD_SERV

From

INSTITUICOES_SERVICOS,

INSTITUICOES,

SERVICOS

where

INSTITUICOES_SERVICOS.FK_CD_INST = INSTITUICOES.CD_INST and

SERVICOS.CD_SERV = INSTITUICOES_SERVICOS.FK_CD_SERV and

cd_serv IN (73,103)

Compartilhar este post


Link para o post
Compartilhar em outros sites

Monte a hierarquia com paretenses

 

(
(cod_inst = 2 and cod_serv = 73) or
(cod_inst = 2 and cod_serv = 103) or
(cod_inst = 2 and cod_serv = 105) or
(cod_inst = 3 and cod_serv = 103) 
)

Compartilhar este post


Link para o post
Compartilhar em outros sites

Monte a hierarquia com paretenses

 

(
(cod_inst = 2 and cod_serv = 73) or
(cod_inst = 2 and cod_serv = 103) or
(cod_inst = 2 and cod_serv = 105) or
(cod_inst = 3 and cod_serv = 103) 
)

Eu entendi, porém se o usuario so escolher

ex:

cod_serv = 73 e cod_serv = 103 (gostaria de saber como faço para fazer um select que me mostre todas as instituicoes que estao vinculas nestes dois servicos.

Compartilhar este post


Link para o post
Compartilhar em outros sites

cleiton,

 

pode postar a estrutura das tabelas?! Qual a versao do SQL?

Tentei montar um exemplo mas sem a estrutura ficou um pouco complicado.

 

Abçs

Compartilhar este post


Link para o post
Compartilhar em outros sites

×

Informação importante

Ao usar o fórum, você concorda com nossos Termos e condições.