MySQL CLAUSE can become a value?

I am new to mysql. I would like to create a new table that is a copy of the original one table with another column under a specific condition. What condition is the new column is the new table. I mean:

Let table be the sequence of a given point (x, y). I want to create a temp table (x, y, r), where r = x ^ 2 + y ^ 2 <1 But I did it

CREATE temp LIKE table;
ALTER TABLE temp ADD r FLOAT;

INSERT INTO temp (x,y) SELECT * FROM table WHERE x*x+y*y<1;
UPDATE temp SET r=x*x+y*y;

This is normal, it gives what I want, but my database is much larger than this simple example, and here I calculate twice the radius r in two tables. This is not good for optimization.

Is there a way to pass the sentence to a new column directly?

Thanks in advance.

+4
source share
3

, . insert update . , .

MySQL having, :

CREATE temp LIKE table;
ALTER TABLE temp ADD r FLOAT;

INSERT INTO temp(x, y, r)
    SELECT x, y, x*x+y*y as r
    FROM table 
    HAVING r < 1;

, , , . , temp, temp , .

, ( , ), - , MySQL.

, . , (, , , ).

+3

() . , , .

, , : ", , ". , - - .

, SQL , - .

CREATE VIEW temp
AS
    SELECT
        x,
        y,
        x*x + y*y AS r
    FROM My_Table
    WHERE
        x*x + y*y < 1
+4

Instead

INSERT INTO temp (x,y) SELECT * FROM table WHERE x*x+y*y<1;
UPDATE temp SET r=x*x+y*y;

Try the following:

INSERT INTO temp (x,y,r) 
SELECT  x, 
       y, 
       x*x+y*y AS r  
FROM table 
WHERE x*x+y*y<1;
0
source

Source: https://habr.com/ru/post/1625206/


All Articles