簡體   English   中英

如何聯接特定的表​​行?

[英]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_onetable_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但我一直收到錯誤消息。

我該如何實現?

您遇到什么錯誤? 也許您只需要在您的critera所在位置的id列上加上別名... – sgeddes

我得到的錯誤是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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM