2
Query that I am using
SELECT
os_id,os_data,os_processo,os_solicitante,os_tipo,os_cliente,os_empresa,os_adverso,os_local,
os_comarca,os_advogado,os_preposto,os_documentos,os_status,login_nome,
sum(osh.os_honorarios_valor) as honorario,sum(osh.os_honorarios_os_despesas) as despesas from os so
inner join os_honorarios osh on so.os_id = osh.os_honorarios_os_id inner join login lo on lo.login_id = so.os_advogado
where so.os_status !=0
Tables:
CREATE TABLE `os_honorarios` (
`os_honorarios_id` INT(11) NOT NULL AUTO_INCREMENT,
`os_honorarios_valor` FLOAT NOT NULL,
`os_honorarios_os_id` INT(11) NOT NULL,
`os_honorarios_os_despesas` FLOAT NULL DEFAULT NULL,
PRIMARY KEY (`os_honorarios_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `os` (
`os_id` INT(11) NOT NULL AUTO_INCREMENT,
`os_data` DATE NOT NULL,
`os_processo` VARCHAR(50) NOT NULL,
`os_solicitante` VARCHAR(50) NOT NULL,
`os_tipo` VARCHAR(50) NOT NULL,
`os_cliente` VARCHAR(50) NOT NULL,
`os_empresa` VARCHAR(50) NOT NULL,
`os_adverso` VARCHAR(50) NOT NULL,
`os_local` VARCHAR(50) NOT NULL,
`os_comarca` VARCHAR(50) NOT NULL,
`os_advogado` VARCHAR(50) NOT NULL,
`os_preposto` VARCHAR(50) NOT NULL,
`os_documentos` VARCHAR(50) NOT NULL,
`os_status` INT(11) NOT NULL,
PRIMARY KEY (`os_id`)
)
COLLATE='latin1_swedish_ci'
ENGINE=InnoDB
;
CREATE TABLE `login` (
`login_id` INT(11) NOT NULL AUTO_INCREMENT,
`login_email` VARCHAR(150) NOT NULL DEFAULT '0',
`login_password` VARCHAR(150) NOT NULL DEFAULT '0',
`login_rule` VARCHAR(150) NOT NULL DEFAULT '0',
`login_nome` VARCHAR(150) NOT NULL DEFAULT '0',
`login_cpf` VARCHAR(50) NOT NULL,
`login_status` INT(11) NOT NULL,
PRIMARY KEY (`login_id`)
)
COLLATE='utf8_general_ci'
ENGINE=InnoDB
AUTO_INCREMENT=9
;
Test I took:
INSERT INTO os (os_id, os_data, os_processo, os_solicitante, os_tipo, os_cliente, os_empresa, os_adverso, os_local, os_comarca, os_advogado, os_preposto, os_documentos, os_status) VALUES (1, '2018-09-06', 't', 't', 't', 't', 't', 't', 't', 't', '8', 't', 't', 1), (2, '2018-09-06', 't', 't', 't', 't', 't', 't', 't', 't', '8', 't', 't', 1);
What I’m trying to do: Unite these three tables and make the display .
<th>ID</th>
<th>Data</th>
<th>processo</th>
<th>solicitante</th>
<th>cliente</th>
<th>empresa</th>
<th>advogado</th>
<th>preposto</th>
<th>despesa</th>
<th>honorarios</th>
<th>Status</th>
<th>Detalhes</th>
<th>Dispesa</th>
<th>honorarios</th>
<th>Desativar</th>
What’s happening is that when I run this query, instead of returning me the expected, returns me empty, already tried to use left join
, but only returned me a single record.
Note: In charge
inner join lo.login_id = so.os_advogado
, the field login_id receives the ID user as well as in lawyer
It would be clearer if you put an example of database data, the current output, and the desired output.
– Giuliana Bezerra
You see, if you only have records in the OS table, INNER JOIN won’t work, you have to do with LEFT JOIN
– Sorack
edit your answer there that worked
– Tulio Vieira
@Sorack was just editing his reply so I could mark it as the right one
– Tulio Vieira
@I restored the tulip tree
– Sorack