[supersaibal]Originariamente inviato da skidx
nel senso, supponiamo che io voglia i dati della quindicesima settimana dell'anno X. Un campo date se non ricordo male è fatto così AAAAMMGG, dovrei quindi fare dei calcoli ad ogni richiesta per ottenere i margini della settimana 15 in termini di GIORNO E MESE.

Avendo invece un campo SETTIMANA con valore 15 la query sarebbe una vera scemenza, e il calcolo per determinare il numero 15 dovrei farlo solo una volta, nell'inserimento, invece che ogni volta nella lettura. [/supersaibal]
Ma perche'? Trovo che sia sicuramente piu' semplice il seguente..

WHERE WEEK(campo_data) = 15

tra l'altro la storia della settimana dell'anno e' tortuosa. Infatti nella versione 4.1 di mysql c'e' una terza funzione in merito.

Ora sono: WEEK, WEEKOFYEAR e YEARWEEK

il problema non e' di mysql ma di come vengono contate le settimane a partire dall'inizio dell'anno. chi inizia di domenica, chi di lunedi', che conta 1-53, chi da 0-52. Dipende da che parte del mondo ti trovi. Quella che ti ho messo (WEEK) ha un secondo parametro 0/1 per dire se la settimana inizia di domenica oppure lunedi', se le settimane sono 0-53 oppure 1-52.
Ti allego la descrizione
WEEK (date[,mode])
The function returns the week number for date. The two-argument form
of WEEK () allows you to specify whether the week starts on Sunday or
Monday and whether the return value should be in the range from 0 to
53 or from 1 to 53. If the mode argument is
omitted, the value of the default_week_format system variable is
used (or 0 before MySQL 4.0.14).
See also : [Server system variables].

The following table describes how the mode argument works:

First day
Mode of week Range Week 1 is the first week...
0 Sunday 0-53 with a Sunday in this year
1 Monday 0-53 with more than 3 days this year
2 Sunday 1-53 with a Sunday in this year
3 Monday 1-53 with more than 3 days this year
4 Sunday 0-53 with more than 3 days this year
5 Monday 0-53 with a Monday in this year
6 Sunday 1-53 with more than 3 days this year
7 Monday 1-53 with a Monday in this year


A mode value of 3 can be used as of MySQL 4.0.5.
Values of 4 and above can be used as of MySQL 4.0.17.