简体   繁体   English

SQL:插入后缺少列

[英]SQL: Missing columns after INSERT

Basically I have two old tables, and new one which combines parts of both by matching ID as referance.基本上我有两个旧表,一个新表通过匹配 ID 作为参考来组合两个部分。

One old table is named " items_table " and contains the following columns: "Material" (which I use as ID to navigate to the items), "Condition", "Quantity", "Desc" (and a bunch of other irrelevant columns for now which I don't use)一个旧表被命名为“ items_table ”并包含以下列:“材料”(我用作 ID 来导航到项目)、“条件”、“数量”、“描述”(以及一堆其他不相关的列现在我不使用)

Second old table is named " price_table " and contains: "ItemNum", "itemtype", "_subtype", "FullPrice" (and more columns which I don't want to include in my new table)第二个旧表名为“ price_table ”,包含:“ItemNum”、“itemtype”、“_subtype”、“FullPrice”(以及我不想包含在新表中的更多列)

And a third new temporary table which is named " Items4sale " and should contain the following columns (values are inserted by query from the old tables): "ID", "_Type", "Subtype", "Condition", "Desc", "Quantity", "FullPrice", "Price" (this is a new calculated value), "PotentiaPrice" (this is a new calculated value)以及名为“ Items4sale ”的第三个新临时表,应包含以下列(值由旧表中的查询插入):“ID”、“_Type”、“Subtype”、“Condition”、“Desc”、 “数量”、“全价”、“价格”(这是一个新的计算值)、“PotentiaPrice”(这是一个新的计算值)

Here's the code:这是代码:

if object_id('Items4sale') is not null  /* <--- creating the new table */
drop table Items4sale;
create table Items4sale (
ID int,
_Type NVARCHAR(100),
Subtype NVARCHAR(100),
Condition INT,
Descr NVARCHAR(100),
Quantity INT,
FullPrice float, /* <--- I actually want to limit the display for prices, for example to "3299.33" instead of "3299.332120" */
Price float, 
PotentialPrice float
)

INSERT INTO Items4sale (ID, _Type, Subtype, FullPrice) /* Inserting here columns from "price_table" */
SELECT [ItemNum], [itemtype], [_subtype], [FullPrice]
FROM price_table WHERE [ItemNum] in (SELECT [Material] from items_table)  /* Same ID of the item should match to one in the items_table */
    INSERT INTO Items4sale (ID, Condition, Quantity, Descr) /* Inserting here columns from "items_table" */
SELECT [Material] (same for ID), [site_loc], [limit], [descriptions]
FROM items_table WHERE cast([site_loc] as CHAR(50)) like '%8';  /* Use only if "site_loc" column (to insert in "Condition" in the new table) ends with "8" ("5338" for example is OK and should be included) */

UPDATE Items4sale set Price=FullPrice*0.05
UPDATE items4sale set PotentialPrice=Price*Quantity

SELECT * FROM Items4sale

This is what I get as a result:这就是我得到的结果:

ID  |  _Type  |  Subtype | Condition  |  Descr  | Quantity | FullPrice | Price | PotentialPrice
189 |  Plastic | Toy     | NULL       | NULL    | NULL     | 8         | 0.4   | NULL
                     .........

Notice how "Condition", "Descr", "Quantity, and "PotentialPrice" come up as NULL. Those matching columns (as in the INSERT in the code above) are not null in the "items_table" where "ID" is "189".注意“Condition”、“Descr”、“Quantity”和“PotentialPrice”是如何出现为NULL的。那些匹配的列(如上面代码中的INSERT)在“items_table”中不为空,其中“ID”为“189” ”。

This is the result that I wanted: (example)这是我想要的结果:(示例)

 ID | _Type | Subtype | Condition | Descr | Quantity | FullPrice | Price | PotentialPrice 189 | Plastic | Toy | 3998 *(ends with '8')* | A plastic toy | 4 | 8 | 0.4 | 1.6 .........

How can I fix this?我怎样才能解决这个问题?

Also, second part of my question - here's a query I ran to test for bugs, (in addition to the code above):另外,我的问题的第二部分 - 这是我运行以测试错误的查询,(除了上面的代码):

SELECT * FROM Items4sale WHERE ID = 189;
SELECT * FROM items_table WHERE [Material] = 189;

The above is to see if there are any missing duplicated IDs in my first query.以上是看我第一次查询时是否有遗漏的重复ID。 Turns out that in "items_table" there is more than one "189" as "Material" (ID of the item) So from the second "SELECT" in the code above I get several rows with "189" as Material (ID) because there's actually more than one - I didn't take into account a column which is named "Location" (NVARCHAR(100)) in this table.原来在“items_table”中有不止一个“189”作为“Material”(项目的ID)所以从上面代码中的第二个“SELECT”我得到了几行“189”作为Material(ID),因为实际上不止一个 - 我没有考虑这个表中名为“位置”(NVARCHAR(100))的列。 I believe this is a bit more advanced: In case there's more than one row which has the ID in "items_table", then don't combine them to one row in "Items4sale" (the combined new table), but instead create them as ID = "189", "(1)189", "(2)189" (just like windows does when you try to create the same file/folder twice) yet their "Condition" (items_table.[site_loc]) in this case doesn't end with '8'.我相信这有点高级:如果“items_table”中有不止一行的 ID,那么不要将它们组合到“Items4sale”(组合的新表)中的一行,而是将它们创建为ID = "189", "(1)189", "(2)189"(就像 Windows 在你尝试创建相同的文件/文件夹两次时所做的那样)但它们的“条件”(items_table.[site_loc])在这个案例不以'8'结尾。 I still want to list them and apply their price and etc from "price_table".我仍然想列出它们并从“price_table”应用它们的价格等。

This is important as the items might be the same (have the same barcode/ID), but have different "Location" and thus probably different "Quantity" aswell.这很重要,因为项目可能相同(具有相同的条形码/ID),但具有不同的“位置”,因此“数量”也可能不同。

Your attempt fails because, basically, you are running 2 insert statements while you want just one record being generated.您的尝试失败了,因为基本上,您正在运行 2 个插入语句,而您只想生成一条记录。

While it should be possible to first insert with the columns from one table, then update with columns from the other one, it is simpler to perform the whole operation at once with an insert ... select query:虽然应该可以首先insert一个表中的列,然后使用另一个表中的列进行update ,但使用insert ... select查询一次执行整个操作会更简单:

insert into items4sale
    (ID, _Type, Subtype, FullPrice, Condition, Quantity, Descr, Price, PotentialPrice)
select
    p.ItemNum, 
    p.ItemType, 
    p.Subtype, 
    p.FullPrice,
    i.site_loc,
    i.limit,
    i.descriptions,
    p.FullPrice * 0.05,
    p.FullPrice * 0.05 * i.limit
from items i
inner join prices p on p.ItemNum = i.Material

Unrelated note: from my understanding of your use case (you want to display derived information), this query would better fit in a view;无关说明:根据我对您的用例(您想显示派生信息)的理解,此查询更适合视图; this gives you an always-up-to-date perspective on your joined data '(on the other hand, a table requires manual updating, which can get tedious).这为您提供了关于已连接数据的始终最新的视角(另一方面,表需要手动更新,这可能会变得乏味)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM