Variable in Mysql VIEW

Posted on 2009-12-24
Last Modified: 2012-08-13
Please peruse the following Query:

SELECT @rownum:=@rownum+1 as rownum, employee_name from (SELECT @rownum:=0) r, employees

This query is used in MySQL to derive ROWNUMBERS (as done in case of Oracle)

My query is that... how can one use this query to create a VIEW in MySQL... when MySQL returns "VARIABLE inside View" error?

Any alternate method of availing ROWNUM in MySQL Views itself?

Thanks in advance.
Question by:achitaley
    LVL 50

    Expert Comment

    by:Steve Bink
    Just throwing this out there...  If your view is meant to be read-only, you do it as the return of a stored procedure instead.
    LVL 20

    Expert Comment

    you can create the "view" as a table instead of a view and get the #s.
    elsewhise, you can't use variables
    LVL 14

    Accepted Solution

    Try this:
    PREPARE stmt FROM "SELECT @rownum:=@rownum+1 as rownum, employee_name from (SELECT @rownum:=0) r, employees";
    EXECUTE stmt;

    For more information:
    LVL 40

    Expert Comment

    try this query. You can create a view using this SQL.

    select (select count(*) from employees e2 where e1.employee_name >= e2.employee_name) as rownum,employee_name
      from employees e1;

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Is Threat Intelligence?

    Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

    This article describes some very basic things about SQL Server filegroups.
    CCModeler offers a way to enter basic information like entities, attributes and relationships and export them as yEd or erviz diagram. It also can import existing Access or SQL Server tables with relationships.
    Video by: Steve
    Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
    Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

    760 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    12 Experts available now in Live!

    Get 1:1 Help Now