Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
240 views
in Technique[技术] by (71.8m points)

mysql - SQL insert into autoincrement

I have an inection

INSERT INTO orders (order_client_name, order_client_surname, order_client_bdate, order_cat_id, order_service_id, order_service_cost) 
    VALUES ('РАрарарара', 'Рахимжанов', '1997-01-31', 1, 12, (select ser_cost FROM services WHERE ser_id=12));

I won't write 'ser_id=...' in select, so can i make autopaste in ser_id what i send in Values 'order_service_id'

Like this:

INSERT INTO orders (order_client_name, order_client_surname, order_client_bdate, order_cat_id, order_service_id, order_service_cost) 
    VALUES ('РАрарарара', 'Рахимжанов', '1997-01-31', 1, 12, (select ser_cost FROM services WHERE ser_id=order_service_id));
question from:https://stackoverflow.com/questions/65861968/sql-insert-into-autoincrement

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Do you want insert . . . select?

INSERT INTO orders (order_client_name, order_client_surname, order_client_bdate, order_cat_id, order_service_id, order_service_cost) 
    SELECT 'РАрарарара', 'Рахимжанов', '1997-01-31', 1, x.order_service_id, s.ser_cost
    FROM services s CROSS JOIN
         (SELECT 12 as order_service_id) x
    WHERE ser_id = x.order_service_id;

You can list the parameters in a subquery, so you only have to pass them once.

That said, it is usually a bad idea to repeat data between tables. In general, you don't need the cost if you have the id. The one exception is if the cost changes over time through updates -- then keeping the cost at the time of the order does make sense.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...