select n,n and 3,n & 3, n mod 4, n and false,n and true from (select 0 n union select 1 union select 2 union select 3 union select 4 union select 5 union select 6 union select 7 union select null) v1; +------+-------+------+-------+-------+-------+ | n | n and | n & | n mod | n and | n and | | | 3 | 3 | 4 | false | true | +------+-------+------+-------+-------+-------+ | 0 | 0 | 0 | 0 | 0 | 0 | | 1 | 1 | 1 | 1 | 0 | 1 | | 2 | 1 | 2 | 2 | 0 | 1 | | 3 | 1 | 3 | 3 | 0 | 1 | | 4 | 1 | 0 | 0 | 0 | 1 | | 5 | 1 | 1 | 1 | 0 | 1 | | 6 | 1 | 2 | 2 | 0 | 1 | | 7 | 1 | 3 | 3 | 0 | 1 | | NULL | NULL | NULL | NULL | 0 | NULL | +------+-------+------+-------+-------+-------+ 9 rows in set (0.00 sec)
And, did you get it right?
“3″ is a true value, that makes “n and true” and “n and 3″ the same. The modulo and bit-operations are obvious and so is the fact, that an operation with NULL results in NULL.
For me the most interesting part is, that TRUE AND NULL is NULL, where as FALSE AND NULL is FALSE.
select avg(unix_timestamp(v1.sold_ts)-unix_timestamp(v2.sold_ts)) as avg_diff from vending_log v1, vending_log v2 where v2.sold_ts = (select sold_ts from vending_log where sold_ts < v1.sold_ts order by 1 desc limit 1 );
]]>
select avg(vv1.diff) from (select *,unix_timestamp(v1.sold_ts) - unix_timestamp((select sold_ts from vending_log where sold_ts < v1 .sold_ts order by sold_ts desc limit 1)) as diff from vending_log v1) vv1;
select date(v1.sold_ts) as dd,sum(v2.price),count(*),
sum(if(v2.price is null,1,0)) as prices_missing
from vending_log v1 left join vending_price v2 on
(v1.product_id = v2.product_id and
date(v1.sold_ts) between v2.valid_from and v2.valid_to)
group by dd;
drop procedure if exists proc_q80;
delimiter //
CREATE PROCEDURE proc_q80 ()
BEGIN
declare v_cnt bigint;
declare v_prev_ts timestamp;
declare v_ts timestamp;
declare v_diff bigint;
declare v_s_diff bigint;
declare v_done smallint;
declare c_vendlog CURSOR FOR SELECT sold_ts FROM vending_log order by sold_ts;
DECLARE CONTINUE HANDLER FOR NOT FOUND SET v_done = 1;
OPEN c_vendlog;
set v_prev_ts = null;
set v_done = 0;
set v_cnt = 0;
set v_s_diff = 0;
REPEAT
FETCH c_vendlog INTO v_ts;
IF v_done = 0 THEN
set v_diff = unix_timestamp(v_ts) - unix_timestamp(v_prev_ts);
IF v_diff is not null THEN
set v_cnt = v_cnt + 1;
set v_s_diff = v_s_diff + v_diff;
END IF;
set v_prev_ts = v_ts;
END IF;
UNTIL v_done = 1 END REPEAT;
close c_vendlog;
SELECT v_s_diff / v_cnt as avg_diff;
END
//
delimiter ;
drop table if exists log1;
set @a:=0;
create table log1
select @a:=@a+1 as row_id,v1.*
from vending_log v1 order by sold_ts;
alter table log1 add primary key(row_id);
select avg(unix_timestamp(v1.sold_ts)-unix_timestamp(v2.sold_ts)) as avg_diff
from log1 v1, log1 v2
where v2.row_id=v1.row_id - 1;
select avg(vv1.diff) from
(select *,unix_timestamp(v1.sold_ts) -
unix_timestamp((select max(sold_ts)
from vending_log
where sold_ts < v1 .sold_ts)) as diff
from vending_log v1) vv1;
The next one is fast, but relies on user variables.
set @a:=null;
set @b:=null;
select avg(vv1.diff) from
(select @b:=@a,@a:=sold_ts, unix_timestamp(sold_ts) - unix_timestamp(@b) as diff
from vending_log order by sold_ts) vv1;
select v1.* from mysql.help_keyword v1,
(select '%TREE%' as sw union select '%TYPE%') v2
where v1.name like v2.sw;
+-----------------+-------+
| help_keyword_id | name |
+-----------------+-------+
| 87 | RTREE |
| 107 | TYPE |
| 441 | BTREE |
| 445 | TYPES |
+-----------------+-------+
4 rows in set (0.03 sec)
Only the “USE_FRM” option of the REPAIR will help you now.
Use this option only as a last resort.
Afterwards, use “SHOW TABLE STATUS” to check the amount of rows and the auto_increment.
If necessary, adjust the value with “ALTER TABLE t1 AUTO_INCREMENT=
As with any REPAIR, make a copy of the table-files first – so that you have something left, in case the REPAIR emtpies your table.