How to make an MYSQL query with WHERE conditional

1

Good morning everyone, I need help and I do not have an idea

I'm putting together a query for a database, which gives me back a result (Table on the left) that contains a list of employees, and the amount that they charged per month. To this query, I have to add a filter, I do not know if a WHERE or should use CASE or IF, so that, if I charge in month "2", do not lift the month 1 record of the same employee, likewise, if not met that condition, that only raises the month "1"

My idea is that the final result, be that of the Table on the right.

I'm not someone to ask for help, or "want them to do their job for him", but in this case, I'm blocked, I can not think of how to do.

In PHP I could do it quietly manipulating multidimensional arrays, but the question is that I have to do it in MySQL, and that's where I block

I hope you can help me, thank you very much everyone!

    
asked by Gabriel Gomez 17.01.2018 в 21:41
source

2 answers

0

The WHERE statement lets you state the conditions of the selection. Example: If we want to select the provider code, name of the provider and its population, for those who are in zone 2. SELECT CODPRO, NAME, POPULATION FROM PROVIDER WHERE ZONE = 2; The columns specified in the SELECT statements and WHERE must belong to the indicated table (s). If characters are used in the logical expression, they must be be among the signs ''. The logical operators are the following: =, & lt ;, & gt ;, > =, < =,! =.

    
answered by 18.01.2018 в 08:46
0

If your month column was numeric, you could get the maximum month in which a person has a record:

SELECT max(mes) as mes, persona FROM tu_tabla GROUP BY persona

And you would get

mes | persona
____|________
1   | Juan
2   | Gabriel
2   | Pedro

Then you could use that subquery to filter your data:

SELECT t1.persona, t1.mes, t1.valor
FROM tu_tabla t1
JOIN (SELECT max(mes) as mes, persona FROM tu_tabla GROUP BY persona) t2 USING (mes, persona);

If your column has the names of the months instead, you will need to go through a dictionary table that relates the number of the month with your name, but I trust that with this answer you will be well oriented.

    
answered by 18.01.2018 в 16:39