I have a ticket table in MySQL, the client can pay a little by card and another part in cash. For that I have a movement table with the movement id that leads to another table (card, cash, current account, etc). But when I want to consult with group by, it returns the tickets once but only shows me the cash delivered:
$query="SELECT * from ticket left join tipocomprobante on
tipocomprobante.idtipocomp=ticket.idtipocomp left join cliente on
ticket.idc=cliente.idcliente left join movimiento on movimiento.idticket=ticket.idticket
WHERE cajero=".$idcajero." AND sesion=".$idsesion." group by ticket.idticket;";
I have tried the query in phpMyAdmin and it does not return the card movements:
CREATE TABLE `movimiento` (
`idmov` int(11) NOT NULL,
`tipomov` int(11) DEFAULT NULL,
`importe` decimal(10,2) DEFAULT NULL,
`idticket` int(11) NOT NULL,
`idcliente` int(11) DEFAULT NULL,
`idsesion` int(11) DEFAULT NULL,
`fecha` varchar(255) COLLATE utf8_spanish_ci DEFAULT NULL
)
CREATE TABLE `ticket` (
`idticket` int(11) NOT NULL,
`fecha` varchar(11) COLLATE utf8_spanish_ci DEFAULT NULL,
`total` decimal(10,2) DEFAULT NULL,
`vuelto` decimal(10,2) DEFAULT NULL,
`hora` varchar(10) COLLATE utf8_spanish_ci DEFAULT NULL,
`idtipocomp` int(11) DEFAULT NULL,
`idc` int(11) NOT NULL,
`cae` varchar(255) COLLATE utf8_spanish_ci DEFAULT NULL,
`fechavto` date DEFAULT NULL,
`cajero` int(11) DEFAULT NULL,
`sesion` int(11) DEFAULT NULL,
`pago` varchar(11) COLLATE utf8_spanish_ci DEFAULT NULL
)
CREATE TABLE `tipomov` (
`idtipomov` int(11) NOT NULL,
`nombre` varchar(100) COLLATE utf8_spanish_ci DEFAULT NULL
)
I have that table where I show the movements of the session. There the idea is that all the tickets appear, with the amount paid in cash, and the amount with a debit or credit card. As you can see, it generates 2 rows in the table for the same ticket, I would like to have each ticket in a single row. Otherwise, you would have to modify the structure of the ticket table and directly add the amount of cash and card. And then check if they are different from 0.
He
group by
will hide info from you because you group by the ticket without using aggregation functions in the other data; the engine doesn't know what to show you, so it shows you the first row of each ticket in the set.Let's say these are the sample tables and data:
I have a ticket, two movements, each one of a type. I removed the joins with customers and vouchers because they seem to be not relevant and I don't have any data either.
If I use a query without grouping:
I get two rows, each with the movement info and the associated ticket data:
I would understand that this is what you are looking for. Now, tell us that you want to do the group by in the ticket. could you make a
And you would get a single row with the ticket AND the total amount of the movements.
The strange thing is: you already have that value in the total row of the ticket because the normalization is not very good (well, you need to have the data there for some non-functional requirement). That's why it's not clear why you say you want to group by ticket AND show all movements . It is exclusionary.