2
I’m trying to group equal lines, but based on a value.
I know it’s possible through a sub-query
, but how I’m using inner join
I couldn’t apply.
SELECT * FROM Concessionarias C inner join Concessionarias_Marcas CM on
C.id = CM.Concessionarias_id and C.Estados_id = 14 group by C.id order by
(Marcas_id = 31) DESC;
This Query returns the following information:
Structure of the BD : Problem:
When I put to group, it groups correctly, but wanted when grouping, to group the item that is with (Marks_id = 31).
Since I have 2 repeated lines, when grouping, he would choose the line where Marcas_id is equal to 31. In this case, it is choosing (Marks_id = 12)
As I could group several equal items , preferably by a field (in the case of Marks_id)?
You only posted the result of your query with group and Inner Join has no way to understand the question got confused, post the structure of the two tables. Maybe I can help!
– Rafael Salomão
Exactly what I want,show all dealerships,but show preferably the Dealerships that have the same customer model. The way it is now, it groups and orders, but when ordering, it orders wrong because the Marcas_id that he took is not the value of the client’s model,ai ends up affecting the order of the list.As I would solve with WHERE?
– saidmrn
I’ve removed to look at some of my darlings here, already I answer again
– Rafael Salomão
Yes you want all records but ordered by the customer brand on the correct front ?
– Rafael Salomão
That, Exactly !
– saidmrn
I went there to look at a darling that I have something similar, however it generates two distinct queries in my code to do. What I know and could help you is the ORDER BY FIELD command (Marcas_id, 31,21,6,12); which provides the sort but to use it would have to have another querie before picking the ids to use inside the command.
– Rafael Salomão
Could you show me the code? ,will solve my problem.
– saidmrn
Let’s go continue this discussion in chat.
– Rafael Salomão