Search code examples
mysqlselectinformation-schema

How a mysql query works on the basis of information schema


I was trying to solve one MySQL query at hackerrank, which demands to draw following pattern:

* * * * * 
* * * * 
* * * 
* * 
*

and in discussions, I found below mysql query, which is fetching data from information_schema to draw the required pattern, which I couldn't understand.

SELECT REPEAT('* ', @NUMBER := @NUMBER - 1) FROM information_schema.tables, (SELECT @NUMBER:=6) t LIMIT 5

Can anyone please explain the flow of this query, how exactly it is working?


Solution

  • The way it works is that info schema dot tables has probably hundreds of rows in it for you.

    You can verify that. It merely wanted some source that has more than 5 rows. It could have been anything.

    It does a cross join to just initialize a variable (@NUMBER) to the number 6. It brings back the repeat starting at 5 asteriks, 1 less per time, for a limit of 5 rows.