Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4991

Warning: Creating default object from empty value in /www/htdocs/v030397/mysql-qotd/wp-content/plugins/sitepress-multilingual-cms/sitepress.class.php on line 4993
Question 39: Which of the alternatives below actually work? – MySQL Question of the Day

Skip to content

By urs in mysql questions

 
Imagine you have to create a running number for a query result and select only the line 1, 2 and 18.
You choose user variables as a means to do that.
 
(Find all correct answers)
 

a) set @a=0;
      select *
      from (select (@a:=@a+1) as ln,table_name
              from information_schema.tables
              where table_schema='mysql' order by table_name) v1
      where ln in (1,2,18);
 
b) set @a=0;
    select (@a:=@a+1) as ln,table_name
      from information_schema.tables
      where table_schema='mysql' and @a in (1,2,18)
      order by table_name;
 
c) set @a=0;
    select @a as ln,table_name
      from information_schema.tables
      where table_schema='mysql' having (@a:=@a+1) in (1,2,18)
      order by table_name; 

 
[ User Variables (5%) - User Variable Properties ]
 

Tags: , ,

Comment Feed

One Response


  1.  
    Answers:
     
    a c
     
    c works, but the most reliable option is a. User variables are powerful but can be tricky.
    Please check http://dev.mysql.com/doc/refman/5.0/en/user-variables.html
     

You must be logged in to post a comment.