[英]How to join a specific table row?
table_one
+------+-------------+
| id | cool |
+------+-------------+
| 1 | 1.58 |
| 2 | 8.88 |
+------+-------------+
table_two
+------+-------------+
| id | okies |
+------+-------------+
| 1 | 2.15 |
| 2 | 7.50 |
+------+-------------+
table_result
+------+-------------------+
| id | result |
+------+-------------------+
| 1 | 1.58 (min value) |
| 2 | 7.50 (min value) |
+------+-------------------+
嗨,我對mysql有點熟悉。 我正在使用minimum least()
函數來查找table_one
和table_two
之間的最小值:
INSERT INTO table_result (id, result)
select table_one.id, least(table_one.cool, table_two.okies) val
from table_one
join table_two on table_one.id = table_two.id
ON DUPLICATE KEY UPDATE id=VALUES(id), result=VALUES(result)
這段代碼可以正常工作,除非我不想(評估)獲得所有表行(id 1
和id 2
)的最小值。 我想評估/定位特定的行ID(例如:僅ID 2
!)。
我嘗試添加WHERE id = 2
但我一直收到錯誤消息。
我該如何實現?
我得到的錯誤是where子句不明確的列'id' 。 在研究了此錯誤之后,我需要指定哪個表ID,因為兩個表都具有ID。
INSERT INTO table_result (id, result)
select table_one.id, least(table_one.cool, table_two.okies) val
from table_one
join table_two on table_one.id = table_two.id
WHERE table_one.id = 2
ON DUPLICATE KEY UPDATE id=VALUES(id), result=VALUES(result)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.