Create a view with column num_rows - MySQL -
i need create view has column named row_num inserted row number, auto increment in normal table.
i'm new views please bare me. let's i've normal table:
| country | name | age | price | -------------------------------- | | john | 22 | 20 | | france | anne | 10 | 15 | | sweden | alex | 49 | 10 |
and on...
the view want create is:
| country | name | price | row_num | ------------------------------------ | | john | 20 | 1 | | france | anne | 10 | 2 | | sweden | alex | 5 | 3 |
and on...
i can generate row_num single select:
select @i:=@i+1 row_num, testing.country, testing.name, testing.price testing testing,(select @i:=0) derivedtable order name
but problem combine query above query creating view. combined query i'm trying:
create or replace view vwx (country, name, price, num_row) select mytable.country, mytable.name, mytable.price, @i:=@i+1 row_number testing testing,(select @i:=0) derivedtable order name;
i following error: #1351 - view's select contains variable or parameter
i know can't use select inside select views don't see other way view way want, i'm sure there way don't know how. functions or procedures i'm new them i'm not comfortable creating functions or procedures in mysql.
i hope made myself clear otherwise i'm more happy explain myself in further detail.
i found solution this:
first create function:
delimiter // create function `func_inc_var_session`() returns int no sql not deterministic begin set @var := @var + 1; return @var; end // delimiter ;
then set @var number want start with. in case zero.
set @var=0;
then create view following:
create or replace view myview (place, name, hour, price, counter) select place, name, hour, price, func_inc_var_session() yourtable input_conditions_here;
the trick here may see null on counter column. if happens please set @var again number , select * again , you'll see counter column populated.
Comments
Post a Comment