Multiple Sum with different conditions in MySQL

Today I was working on my last project that involves some statistics on data. I had a table like this:

sample table

]1 My table, with time and data fields.

I needed the daily, monthly and yearly sum of the value field. The simplest approach was to have three different queries to retrieve the needed values:

--day
SELECT SUM(value) 
FROM table 
WHERE DATE(time)=CURDATE();
-- month
SELECT SUM(value) 
FROM table 
WHERE MONTH(time)=MONTH(CURDATE()) AND YEAR(time)=YEAR(CURDATE());
-- year
SELECT SUM(value) 
FROM table 
WHERE YEAR(time)=YEAR(CURDATE());

But I wasn’t satisfied. I wanted to have all three values using only one query. So I searched for this issue and this is the resulting query:

SELECT 
  SUM(CASE WHEN DATE(time)=CURDATE() 
    THEN value 
    ELSE 0 end) AS value_day,
  SUM(CASE WHEN MONTH(time)=MONTH(CURDATE()) AND YEAR(time)=YEAR(CURDATE()) 
    THEN value 
    ELSE 0 end) AS value_month,
  SUM(CASE WHEN YEAR(time)=YEAR(CURDATE()) 
    THEN value 
    ELSE 0 end) AS value_year
FROM table

Using the CASE construct, we increment independently the three values so that each row that satisfies the condition gets summed up.

Come cambiare solo la data in un campo DATETIME di MySQL?

Oggi mi sono trovato a dover risolvere questo problema: aggiornare un campo DATETIME di una tabella, aggiungendo un giorno, lasciando invariata l’ora. Cioè da questo 2012-10-14 00:15:00 volevo ottenere questo 2012-10-15 00:15:00. Il comando SQL è molto semplice:

UPDATE `tbl_dati` SET 
  ora = ora + INTERVAL 1 DAY;

Ovviamente al posto di DAY è possibile utilizzare un’altra unità di misura di tempo, come indicato nella documentazione di MySQL

Visualizzare select di tabelle con molti campi in MySQL

Se capita di dover effettuare una SELECT su una tabella da linea di comando, i risultati verranno visualizzati in una tabella in formato ASCII.
Nel caso in cui la tabella contiene molti campi, questa tabella risulterà non formattata correttamente, con righe spezzate su più livelli.
Per visualizzare un record alla volta si può utilizzare il flag \G dopo il comando.

Ad esempio:

mysql> select * from prova;
+----+---------+--------+------+
| id | testo   | numero | car  |
+----+---------+--------+------+
|  1 | Uno     |      0 | r    |
|  2 | Due     |      0 | r    |
|  3 | Tre     |      0 | r    |
|  4 | Quattro |      0 | r    |
|  5 | Cinque  |      0 | r    |
|  6 | Sei     |      0 | r    |
|  7 | Sette   |      0 | r    |
|  8 | Otto    |      0 | r    |
|  9 | Nove    |      0 | r    |
+----+---------+--------+------+
9 rows in set (0.00 sec)


mysql> select * from prova\G
*************************** 1. row ***************************
    id: 1
 testo: Uno
numero: 0
   car: r
*************************** 2. row ***************************
    id: 2
 testo: Due
numero: 0
   car: r
*************************** 3. row ***************************
    id: 3
 testo: Tre
numero: 0
   car: r
*************************** 4. row ***************************
    id: 4
 testo: Quattro
numero: 0
   car: r
*************************** 5. row ***************************
    id: 5
 testo: Cinque
numero: 0
   car: r
*************************** 6. row ***************************
    id: 6
 testo: Sei
numero: 0
   car: r
*************************** 7. row ***************************
    id: 7
 testo: Sette
numero: 0
   car: r
*************************** 8. row ***************************
    id: 8
 testo: Otto
numero: 0
   car: r
*************************** 9. row ***************************
    id: 9
 testo: Nove
numero: 0
   car: r
9 rows in set (0.00 sec)

Update con Join in MySQL

Mi sono trovato a dover aggiornare un attributo di una relazione, avendo come input l’identificativo di un’altra relazione, collegata alla prima tramite vincolo di chiave esterna.
In MySQL:

UPDATE tabella1 t1 JOIN tabella2 t2
ON t1.id=t2.attributoConFK SET t1.campo=t1.campo+1;