Closed. This question needs details or clarity . It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post .
Closed 2 days ago.
Improve this question
I have a table with 5 columns enter image description here like this. The 1st, 2nd and 3rd column was entered from keyboard The 4th column is insert from other table The 5th column = 2nd column * 3rd column. How can I insert this? My code is wrong can u fix that. Thanks everyone
How can I insert this? My code is wrong can u fix that. Thanks everyone
2条答案
按热度按时间rqenqsqc1#
You have:
The indention shows that you are getting deeper and deeper, because you forgot the closing parentheses and commas after each tuple. This is the code corrected:
This would make the syntax valid. It is tuples of five values that your are inserting. The first tuple consists of the values 2, 4, and 3, and the value in dongia from hanghoa, and the result of soluong * dongia from chitietdonhang. So you expect the two subqueries to return one value. Do the two tables have only one row each? Otherwise this cannot work, because each subquery represents one value in the insert statement. If there is more than one row in the tables, you would have to add a where clause to pick the row you want or aggregate the table data to a single result row.
Update
I've tried Google Translate. The table you are inserting to is Chi Tiết Đơn Hàng = order details? With Số Lượng = quantity, Đơn Giá = unit price, Thanh Toán = total price? And Hàng Hóa is the product table and MaHH = Mã Hàng Hóa = product code?
In that case you want to select the unit price from the product table. But you must do that for the product you are inserting:
But then, if the total price is the result of two columns in the same table row, you should not store it. If you store quantity (Số Lượng) = 5 and unit price (Đơn Giá) = 10, then the total (Thanh Toán) must be 50, correct? Storing this would mean redundancy and a dependency of one column from others. That must not be. Database normalization forbids this. I suggest you remove this column from your table. You always quickly calculate it when needed. Or you make this a computed column that you never fill with a value, because it gets its value automatically from the other columns:
The statement would then be reduced to:
kninwzqo2#
You must specify the following
1.filter two tables
2.Which record should be extracted?(I temporary use Top 1)
3.numbers is hardcode(if isn't you must use decclare exmaple decclare @Madh int)
create table :