Usamos cookies para medir audiência e melhorar sua experiência. Você pode aceitar ou recusar a qualquer momento. Veja sobre o iMasters.
Boa tarde a todos.
Possuo duas tabelas conforme abaixo:
TABELA: gc_prdts_qnt_info
--
-- Estrutura da tabela gc_prdts_qnt_info
--
CREATE TABLE IF NOT EXISTS `gc_prdts_qnt_info` (
`PQI_IdPrdtsQnt` int(10) NOT NULL DEFAULT '0',
`PQI_IdInfo` int(10) NOT NULL DEFAULT '0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
--
-- Extraindo dados da tabela gc_prdts_qnt_info
--
INSERT INTO gc_prdts_qnt_info (PQI_IdPrdtsQnt, PQI_IdInfo) VALUES
(1, 290),
(2, 291),
(3, 292),
(4, 290),
(5, 291),
(6, 292),
(7, 290),
(8, 291),
(9, 292),
(10, 290),
(11, 291),
(12, 292),
(1, 261),
(2, 261),
(3, 261),
(4, 274),
(5, 274),
(6, 274),
(7, 275),
(8, 275),
(9, 275),
(10, 276),
(11, 276),
(12, 276);
TABELA: gc_prdts_qnt
--
-- Estrutura da tabela gc_prdts_qnt
--
CREATE TABLE IF NOT EXISTS `gc_prdts_qnt` (
`PQ_IdPrdtsQnt` int(10) NOT NULL AUTO_INCREMENT,
`PQ_IdPrdts` int(10) NOT NULL DEFAULT '0',
`PQ_Qnt` int(5) NOT NULL DEFAULT '0',
`PQ_QntMaxCart` int(5) NOT NULL DEFAULT '0',
`PQ_QntMinEstq` int(5) NOT NULL DEFAULT '0',
`PQ_Preco` decimal(10,2) NOT NULL DEFAULT '0.00',
`PQ_PrecoSts` char(3) NOT NULL DEFAULT 'Nao',
PRIMARY KEY (`PQ_IdPrdtsQnt`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=15 ;
--
-- Extraindo dados da tabela gc_prdts_qnt
--
INSERT INTO gc_prdts_qnt (PQ_IdPrdtsQnt, PQ_IdPrdts, PQ_Qnt, PQ_QntMaxCart, PQ_QntMinEstq, PQ_Preco, PQ_PrecoSts) VALUES
(1, 621, 3, 3, 2, '0.00', 'Nao'),
(2, 621, 5, 4, 2, '0.00', 'Nao'),
(3, 621, 11, 10, 5, '0.00', 'Nao'),
(4, 621, 10, 5, 3, '0.00', 'Nao'),
(5, 621, 15, 10, 5, '0.00', 'Nao'),
(6, 621, 20, 12, 6, '0.00', 'Nao'),
(7, 621, 8, 5, 6, '0.00', 'Nao'),
(8, 621, 16, 10, 5, '0.00', 'Nao'),
(9, 621, 28, 13, 6, '0.00', 'Nao'),
(10, 621, 30, 20, 8, '35.00', 'Sim'),
(11, 621, 35, 26, 5, '42.69', 'Sim'),
(12, 621, 40, 30, 10, '70.25', 'Sim'),
(13, 744, 14, 10, 6, '0.00', 'Nao'),
(14, 641, 26, 15, 6, '0.00', 'Nao');
Para estas tabelas preciso fazer uma consulta seguindo alguns parâmetros e tentei o sql abaixo:
SELECT
gc_prdts_qnt_info.PQI_IdInfo,
gc_prdts_qnt.PQ_IdPrdtsQnt, gc_prdts_qnt.PQ_IdPrdts, gc_prdts_qnt.PQ_Preco, gc_prdts_qnt.PQ_PrecoSts, gc_prdts_qnt.PQ_Qnt AS Qnt, gc_prdts_qnt.PQ_QntMaxCart AS QntMaxCart
FROM gc_prdts_qnt_info
RIGHT JOIN gc_prdts_qnt ON gc_prdts_qnt.PQ_IdPrdtsQnt = gc_prdts_qnt_info.PQI_IdPrdtsQnt
WHERE gc_prdts_qnt.PQ_IdPrdts = '621'
AND gc_prdts_qnt_info.PQI_IdInfo IN('276','292')
Porém estou recebendo uma resposta que levou em consideração a condição "OR"
A minha necessidade é que se retorne um resultado onde (gc_prdts_qnt.PQ_IdPrdts = '621' e gc_prdts_qnt_info.PQI_IdInfo = '276') e (gc_prdts_qnt.PQ_IdPrdts = '621' e gc_prdts_qnt_info.PQI_IdInfo = '292')
a minha resposta ideal seriam dois registros, ambos com o PQI_IdPrdtsQnt = '12'
tentei também a consulta abaixo:
SELECT
gc_prdts_qnt_info.PQI_IdInfo,
gc_prdts_qnt.PQ_IdPrdtsQnt, gc_prdts_qnt.PQ_IdPrdts, gc_prdts_qnt.PQ_Preco, gc_prdts_qnt.PQ_PrecoSts, gc_prdts_qnt.PQ_Qnt AS Qnt, gc_prdts_qnt.PQ_QntMaxCart AS QntMaxCart
FROM gc_prdts_qnt_info
LEFT JOIN gc_prdts_qnt ON gc_prdts_qnt.PQ_IdPrdtsQnt = gc_prdts_qnt_info.PQI_IdPrdtsQnt
WHERE gc_prdts_qnt_info.PQI_IdInfo in (SELECT gc_prdts_qnt_info.PQI_IdInfo FROM gc_prdts_qnt_info INNER JOIN gc_prdts_qnt ON gc_prdts_qnt.PQ_IdPrdtsQnt = gc_prdts_qnt_info.PQI_IdPrdtsQnt Where gc_prdts_qnt.PQ_IdPrdts = '621' AND gc_prdts_qnt_info.PQI_IdInfo = '292')
OR gc_prdts_qnt_info.PQI_IdInfo in (SELECT gc_prdts_qnt_info.PQI_IdInfo FROM gc_prdts_qnt_info INNER JOIN gc_prdts_qnt ON gc_prdts_qnt.PQ_IdPrdtsQnt = gc_prdts_qnt_info.PQI_IdPrdtsQnt Where gc_prdts_qnt.PQ_IdPrdts = '621' AND gc_prdts_qnt_info.PQI_IdInfo = '276')
para todas as consultas o retorno não é o que eu necessito, alguém teria alguma ideia de como realizar esta consulta?
Obrigado
Mauro Lúcio
Carregando comentários...