tags:

views:

35

answers:

3

I have a table

id_user, hour, medition  
1 0  100  
1 1  101  
1 14 102  
2 5  108  
2 17 103

How I can complete the inexistent hours with a 0 value for any user?
example

1 0 100  
1 1 101  
1 2 0  
1 3 0  
...
+2  A: 

You shouldn't do this, as it's redundant. Only add entries where someone has hours. In your program, just code it so that if a record is not found, then there are zero hours. An example, in PHP:

$row = mysql_fetch_array(mysql_query("select * from myTable where id_user = 1 and hour = 3;"));
if (!$row) $hours = 0;
else $hours = $row['medition'];
Delan Azabani
+1 common suggestion.
Jason McCreary
I search a MySQL answer
JuanPablo
What do you mean?
Delan Azabani
A answer to MySQL level, no in other language
JuanPablo
+1, this is THE answer, unless you can justify why you want to run down an inferior path.
Dolph
@JuanPablo, according to my answer, **you don't need to do anything at all**, especially not in MySQL. The code presented was merely a PHP binding example of how to modify your search code to take into account missing records.
Delan Azabani
+1  A: 

build a table hours, populate it with the numbers from 1 to 24, then use this to query with an outer join against your primary table. - replace nulls with 0

edit to add quick example:

SELECT id_user, hour, NVL(medition,0)
FROM `hours` as h
LEFT JOIN `myTable`
    ON `hours`.`hour` = `myTable`.`hour`
Randy
you have a example?
JuanPablo
the example now included should be close.
Randy
A: 

Check out my solution to a similar problem regarding dates at;

http://stackoverflow.com/questions/3093924

You can create a table for the hours, as suggested by Randy, instead of the dates that I created, should be easy enough then to modify the query to return the data you need...

Hope it helps!

Dave Rix