V
V
Vladimir Korotenko2020-11-14 16:28:57
PHP
Vladimir Korotenko, 2020-11-14 16:28:57

How best to make a multiplication table?

My version.

SELECT 
    @N:[email protected] + 1 AS '',
    (@N * 1) AS '1',
    (@N * 2) AS '2',
    (@N * 3) AS '3',
    (@N * 4) AS '4',
    (@N * 5) AS '5',
    (@N * 6) AS '6',
    (@N * 7) AS '7',
    (@N * 8) AS '8',
    (@N * 9) AS '9',
    (@N * 10) AS '10'
FROM
    mysql.help_relation,
    (SELECT @N:=0) dum
LIMIT 10;


Just offer yours. This is not a question, not an interview, just curious. Both the simplest options and the brainfuck style are welcome

Answer the question

In order to leave comments, you need to log in

4 answer(s)
I
Ivan Koryukov, 2016-08-11
@MadridianFox

If you create a bunch of identical tables with different names, then you have problems not with the query as such, but with the approach to working with the database.
What prevents to create one table in which in addition there will be a field user?
Well, in a similar way to insert data into a query is also not good. Any user can send a piece of sql code in POST instead of the table name, which will either delete the entire database or allow you to see confidential data.

V
Vitaly, 2016-08-11
@KotSlon

All wrong. :) google about "prepared statement" and "sql injection"

S
Sergey Zelensky, 2016-08-11
@SergeyZelensky-Rostov

The incorrect table name is written, just write without `wert`, and you don’t have a hole, but a hole for performing sql injection, use PDO or Mysqli and filter the input data UPD
: users
and write everything you need into it, you should not produce a bunch of tables
, here is the code in which your version works

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <title>Document</title>
</head>
<body>
  <form action="" method="POST">
    <input type="text" name="user">
    <input type="submit" value="submit">
    <?php 
    $user = $_POST['user'];
    $w = "CREATE TABLE `".$user."` (`ID` char(255) not null, `Page` char(255), `Class` char(255),`Top` float,`Left` float, PRIMARY KEY(`ID`))";
    $link = mysql_connect('localhost', 'root', '');
    mysql_select_db('test-new',$link);
    echo mysql_query($w);
    

      

    ?>
  </form>
</body>
</html>

L
Lazy @BojackHorseman, 2020-11-14
@firedragon

nothing will be faster

SELECT
 CONCAT(l.factor, ' x ', r.factor), 
 l.factor * r.factor
FROM (SELECT 1 as factor UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) l
JOIN (SELECT 1 as factor UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 10) r ON 1 = 1 
ORDER BY l.factor, r.factor

but will not work on one popular subd. and not because CONCAT

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question