1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
| mysql> create table Orders -> (order_id int unsigned not null auto_increment, -> product_name varchar(30) not null, -> price decimal(8,2) not null, -> amount smallint not null, -> primary key(order_id)); Query OK, 0 rows affected (0.13 sec)
mysql> insert into Orders values -> (null, 'cpu', 135.5 ,1), -> (null, 'memory', 48.2, 3), -> (null, 'cpu', 125.6, 3), -> (null, 'cpu', 105.3, 4); Query OK, 4 rows affected (0.06 sec) Records: 4 Duplicates: 0 Warnings: 0
mysql> select * from Orders; + | order_id | product_name | price | amount | + | 1 | cpu | 135.50 | 1 | | 2 | memory | 48.20 | 3 | | 3 | cpu | 125.60 | 3 | | 4 | cpu | 105.30 | 4 | + 4 rows in set (0.00 sec)
mysql> create table Orders_MV -> ( product_name varchar(30) not null, -> price_sum decimal(8,2) not null, -> amount_sum int not null, -> price_avg float not null, -> orders_cnt int not null, -> unique index (product_name)); Query OK, 0 rows affected (0.14 sec)
mysql> insert into Orders_MV -> select product_name, sum(price), -> sum(amount), avg(price), count(*) -> from Orders -> group by product_name; Query OK, 2 rows affected (0.07 sec) Records: 2 Duplicates: 0 Warnings: 0
mysql> select * from Orders_MV; + | product_name | price_sum | amount_sum | price_avg | orders_cnt | + | cpu | 366.40 | 8 | 122.133 | 3 | | memory | 48.20 | 3 | 48.2 | 1 | + 2 rows in set (0.00 sec)
delimiter //
CREATE TRIGGER tgr_Orders_insert AFTER INSERT ON Orders FOR EACH ROW BEGIN SET @old_price_sum := 0; SET @old_amount_sum := 0; SET @old_price_avg := 0; SET @old_orders_cnt := 0; SELECT IFNULL(price_sum, 0), IFNULL(amount_sum, 0), IFNULL(price_avg, 0), IFNULL(orders_cnt, 0) FROM Orders_MV WHERE product_name = NEW.product_name INTO @old_price_sum , @old_amount_sum , @old_price_avg , @old_orders_cnt;
SET @new_price_sum = @old_price_sum + NEW.price; SET @new_amount_sum = @old_amount_sum + NEW.amount; SET @new_orders_cnt = @old_orders_cnt + 1; SET @new_price_avg = @new_price_sum / @new_orders_cnt ; REPLACE INTO Orders_MV VALUES(NEW.product_name, @new_price_sum, @new_amount_sum, @new_price_avg, @new_orders_cnt ); END;//
delimiter ;
mysql> insert into Orders values (null, 'ssd', 299, 3); Query OK, 1 row affected (0.03 sec)
mysql> insert into Orders values (null, 'memory', 47.9, 5); Query OK, 1 row affected (0.05 sec)
mysql> select * from Orders_MV; + | product_name | price_sum | amount_sum | price_avg | orders_cnt | + | cpu | 366.40 | 8 | 122.133 | 3 | | memory | 96.10 | 8 | 48.05 | 2 | | ssd | 299.00 | 3 | 299 | 1 | + 3 rows in set (0.00 sec)
mysql> select @test; + | @test | + | NULL | + 1 row in set (0.00 sec)
mysql> select ifnull(@test, 100); + | ifnull(@test, 100) | + | 100 | + 1 row in set (0.00 sec)
mysql> select @test; + | @test | + | NULL | + 1 row in set (0.00 sec)
mysql> set @test:=200; Query OK, 0 rows affected (0.00 sec)
mysql> select ifnull(@test, 100); -- 再次ifnull判断,此时test不为null,则返回test变量的值 + | ifnull(@test, 100) | + | 200 | + 1 row in set (0.00 sec)
mysql> select @id_1; + | @id_1 | + | NULL | + 1 row in set (0.00 sec)
mysql> select @score_1; + | @score_1 | + | NULL | + 1 row in set (0.00 sec)
mysql> select * from test_rank_2; + | id | score | + | 1 | 10 | | 2 | 20 | | 3 | 30 | | 4 | 30 | | 5 | 40 | | 6 | 40 | + 6 rows in set (0.00 sec)
mysql> select * from test_rank_2 -> where id=1 into @id_1, @score_1;
Query OK, 1 row affected (0.00 sec)
mysql> select @id_1; + | @id_1 | + | 1 | + 1 row in set (0.00 sec)
mysql> select @score_1; + | @score_1 | + | 10 | + 1 row in set (0.00 sec)
|