MySQL: 如何在更新值时使该子查询起作用?
MySQL: How can I make this subquery work while updating a value?
我正在尝试编写一个列出项目 ID、标题、旧价格和新价格的查询。我不能做一个更新声明,因为那样我不认为我可以列出旧价格。
说明:
USE A SUBQUERY to Increase price of all items by ‘No Rest for the Weary’ by 10%. Show prices before and after. Rollback after.
根据说明,此作业有三个主要目标:
1.显示旧价。
2. 使用子查询通过计算显示新价格。
3. 完成后使用回滚。
问题:
是否可以像子查询一样将 UPDATE 放在 SELECT 语句中?
(答案显然是否定的。)
我 运行 遇到的问题:
UPDATE items
SET unit_price = ROUND(unit_price + (unit_price * .10),2) as 'New Price'
WHERE item_id =
(SELECT item_id as 'Item ID',
title as 'Title',
unit_price as 'Old Price', --Except this won't work because the unit price is now the new price...
FROM items
WHERE title = 'No Rest for the Weary'
);
这就是我现在拥有的,但是 ROLLBACK 位让我卡住了。在这种情况下,人们会把它放在哪里?还是我完全误解了说明?
SELECT item_id as 'Item ID', title as 'Title', unit_price as 'Price',
ROUND(unit_price + (unit_price * .10),2) as 'New Price'
FROM items
WHERE item_id =
(SELECT item_id
FROM items
WHERE title = 'No Rest for the Weary'
);
不,不可能在 SELECT
语句中包含 UPDATE
语句。但是可以在 UPDATE
.
中包含 SELECT
为什么需要 UPDATE
语句? 更新 table 中的一个列是否有新值,以便保留新值?
或者,return 的意图是结果集吗? UPDATE
语句不像 SELECT
语句那样 return 结果集。
问题中的 UPDATE
语句将不起作用,因为子查询 return 编辑了三列。在上下文中使用
col IN (subquery)
子查询应该return只有一列。 return 两列或更多列无效。
您可以编写一个 SELECT 语句,其中 return 是表达式的结果。在此示例中,新单价比当前单价增加 10% unit_price...
SELECT i.item_id AS `item_id`
, i.title AS `title`
, i.unit_price AS `old_unit_price`
, ROUND(i.unit_price*1.1,2) AS `new_unit_price`
FROM items i
WHERE i.title = 'No Rest for the Weary'
ORDER BY i.item_id
如果那是 return 您想要的结果,并且您想在 UPDATE
中使用它来为 unit_price
列分配一个新值,假设 item_id
是 items
table...
上的 PRIMARY 或 UNIQUE KEY
UPDATE ( SELECT r.*
FROM ( -- select statement above goes here
SELECT i.item_id AS `item_id`
, i.title AS `title`
, i.unit_price AS `old_unit_price`
, ROUND(i.unit_price*1.1,2) AS `new_unit_price`
FROM items i
WHERE i.title = 'No Rest for the Weary'
) r
) s
JOIN items t
ON t.item_id = s.item_id
SET t.unit_price = s.new_unit_price
再次强调这一点,这假设 item_id
是 items
table.[=35= 上的 PRIMARY KEY(或非 NULL UNIQUE KEY) ]
在 UPDATE
语句之后,重新 运行 原始 SELECT 查询将 return 不同的结果(假设原始 unit_price 足够大于零。)
跟进
- 创建显示旧价格的查询
上面答案中的第一个 SELECT 显示 "old price"(假设 "old price" 存储在 unit_price
列中。)
- 将价格更改为比当前价格高 10%。
上面答案中的第一个 SELECT 显示将 10% 添加到 unit_price
列,四舍五入到小数点后两位数,returned 作为另一列 new_unit_price
.
- 使用子查询
第一个查询没有使用子查询。但是我们可以很容易地添加一个。 (我不明白为什么我们需要添加一个不必要的子查询。子查询 returns 有什么关系,我们可以在 SELECT 列表或 WHERE 子句中使用子查询吗?内联视图有资格作为子查询吗?)
此版本在 SELECT 列表中添加了两个不必要的相关子查询、一个不必要的内联视图和 WHERE 子句中的一个不必要的子查询。
SELECT i.item_id AS `item_id`
, ( SELECT t.title
FROM items t
WHERE t.item_id = i.item_id
ORDER BY t.title
LIMIT 1
) AS `title`
, ( SELECT p.unit_price
FROM items p
WHERE p.item_id = i.item_id
ORDER BY p.unit_price
LIMIT 1
) AS `old_unit_price`
, ROUND(i.unit_price*1.1,2) AS `new_unit_price`
FROM items i
CROSS
JOIN ( SELECT 1 AS i ) i
WHERE i.title = (SELECT 'No Rest for the Weary')
ORDER BY i.item_id
UPDATE
语句的更简单版本(将 10% 添加到 unit_price)也不需要子查询。
UPDATE items t
SET t.unit_price = ROUND(t.unit_price*1.1,2)
WHERE t.title = 'No Rest for the Weary'
同样,UPDATE
语句不可能 return 结果集。
我正在尝试编写一个列出项目 ID、标题、旧价格和新价格的查询。我不能做一个更新声明,因为那样我不认为我可以列出旧价格。
说明:
USE A SUBQUERY to Increase price of all items by ‘No Rest for the Weary’ by 10%. Show prices before and after. Rollback after.
根据说明,此作业有三个主要目标: 1.显示旧价。 2. 使用子查询通过计算显示新价格。 3. 完成后使用回滚。
问题: 是否可以像子查询一样将 UPDATE 放在 SELECT 语句中? (答案显然是否定的。)
我 运行 遇到的问题:
UPDATE items
SET unit_price = ROUND(unit_price + (unit_price * .10),2) as 'New Price'
WHERE item_id =
(SELECT item_id as 'Item ID',
title as 'Title',
unit_price as 'Old Price', --Except this won't work because the unit price is now the new price...
FROM items
WHERE title = 'No Rest for the Weary'
);
这就是我现在拥有的,但是 ROLLBACK 位让我卡住了。在这种情况下,人们会把它放在哪里?还是我完全误解了说明?
SELECT item_id as 'Item ID', title as 'Title', unit_price as 'Price',
ROUND(unit_price + (unit_price * .10),2) as 'New Price'
FROM items
WHERE item_id =
(SELECT item_id
FROM items
WHERE title = 'No Rest for the Weary'
);
不,不可能在 SELECT
语句中包含 UPDATE
语句。但是可以在 UPDATE
.
SELECT
为什么需要 UPDATE
语句? 更新 table 中的一个列是否有新值,以便保留新值?
或者,return 的意图是结果集吗? UPDATE
语句不像 SELECT
语句那样 return 结果集。
问题中的 UPDATE
语句将不起作用,因为子查询 return 编辑了三列。在上下文中使用
col IN (subquery)
子查询应该return只有一列。 return 两列或更多列无效。
您可以编写一个 SELECT 语句,其中 return 是表达式的结果。在此示例中,新单价比当前单价增加 10% unit_price...
SELECT i.item_id AS `item_id`
, i.title AS `title`
, i.unit_price AS `old_unit_price`
, ROUND(i.unit_price*1.1,2) AS `new_unit_price`
FROM items i
WHERE i.title = 'No Rest for the Weary'
ORDER BY i.item_id
如果那是 return 您想要的结果,并且您想在 UPDATE
中使用它来为 unit_price
列分配一个新值,假设 item_id
是 items
table...
UPDATE ( SELECT r.*
FROM ( -- select statement above goes here
SELECT i.item_id AS `item_id`
, i.title AS `title`
, i.unit_price AS `old_unit_price`
, ROUND(i.unit_price*1.1,2) AS `new_unit_price`
FROM items i
WHERE i.title = 'No Rest for the Weary'
) r
) s
JOIN items t
ON t.item_id = s.item_id
SET t.unit_price = s.new_unit_price
再次强调这一点,这假设 item_id
是 items
table.[=35= 上的 PRIMARY KEY(或非 NULL UNIQUE KEY) ]
在 UPDATE
语句之后,重新 运行 原始 SELECT 查询将 return 不同的结果(假设原始 unit_price 足够大于零。)
跟进
- 创建显示旧价格的查询
上面答案中的第一个 SELECT 显示 "old price"(假设 "old price" 存储在 unit_price
列中。)
- 将价格更改为比当前价格高 10%。
上面答案中的第一个 SELECT 显示将 10% 添加到 unit_price
列,四舍五入到小数点后两位数,returned 作为另一列 new_unit_price
.
- 使用子查询
第一个查询没有使用子查询。但是我们可以很容易地添加一个。 (我不明白为什么我们需要添加一个不必要的子查询。子查询 returns 有什么关系,我们可以在 SELECT 列表或 WHERE 子句中使用子查询吗?内联视图有资格作为子查询吗?)
此版本在 SELECT 列表中添加了两个不必要的相关子查询、一个不必要的内联视图和 WHERE 子句中的一个不必要的子查询。
SELECT i.item_id AS `item_id`
, ( SELECT t.title
FROM items t
WHERE t.item_id = i.item_id
ORDER BY t.title
LIMIT 1
) AS `title`
, ( SELECT p.unit_price
FROM items p
WHERE p.item_id = i.item_id
ORDER BY p.unit_price
LIMIT 1
) AS `old_unit_price`
, ROUND(i.unit_price*1.1,2) AS `new_unit_price`
FROM items i
CROSS
JOIN ( SELECT 1 AS i ) i
WHERE i.title = (SELECT 'No Rest for the Weary')
ORDER BY i.item_id
UPDATE
语句的更简单版本(将 10% 添加到 unit_price)也不需要子查询。
UPDATE items t
SET t.unit_price = ROUND(t.unit_price*1.1,2)
WHERE t.title = 'No Rest for the Weary'
同样,UPDATE
语句不可能 return 结果集。