在这个查询中我想更新那些最新发布的记录。但是我的这个查询不起作用请帮帮我是什么原因???
错误: - 您无法在FROM子句
中指定目标表'beevers_products'以进行更新update beevers_products set product_name='my_product_name' where posted_date in (SELECT posted_date FROM `beevers_products` order by posted_date asc limit 1)
答案 0 :(得分:1)
检查一下:
UPDATE beevers_products
SET product_name = 'my_product_name'
WHERE posted_date = (SELECT posted_date
FROM beevers_products
ORDER BY posted_date DESC limit 1)
答案 1 :(得分:0)
建议您使用CI Active Record类进行查询。
http://ellislab.com/codeigniter/user-guide/database/active_record.html
答案 2 :(得分:0)
试试这个:
update beevers_products as t1,
(select posted_date from beevers_products order by posted_date asc limit 1) as t2
set t1.product_name = 'my_product_name'
where t1.posted_date in (t2.posted_date);
您必须为所需的记录提供别名,并在where子句中使用它。
答案 3 :(得分:0)
试试这个
UPDATE beevers_products
SET product_name = 'my_product_name'
OUTPUT DELETED.product_name
WHERE posted_date in (SELECT posted_date
FROM `beevers_products`
order by posted_date asc
limit 1)
详细了解output
选中此项 - > Can I update/select from a table in one query?
可能对你有所帮助
答案 4 :(得分:0)
INSERT INTO beevers_products (id, product_name)
SELECT id, 'my_product_name'
FROM beevers_products
ORDER BY posted_date ASC
LIMIT 1
ON DUPLICATE KEY UPDATE product_name = VALUES(product_name)
一旦我学会了INSERT ... SELECT ... ON DUPLICATE
这么多可能性浮出水面。只要你想要posted_data ASC
或posted_data DESC
,我就会有点好奇。