UPDATE table1其中table2为多个变量赋值

时间:2012-03-03 17:15:38

标签: sql

您好我想要将名为'灯具'的表格中的值从fixtures.toggle=hidden更新为fixtures.toggle=visible
它应该只更新fixtures.compid是以下competition.id之一的灯具 在一个名为“竞争”的单独表格中。

SELECT * 
FROM `competitions` 
WHERE `Year` LIKE CONVERT( _utf8 '2012' USING latin1 ) COLLATE latin1_swedish_ci 
AND ( `countyid` =4 OR `countyid` =11 OR `countyid` =20 OR `countyid` =22 )

我对SQL很陌生,并希望在正确的方向上找到一点。

1 个答案:

答案 0 :(得分:1)

因此,如果我理解,您希望更新上述查询返回的fixtures.compid中包含competitions.id的行。使用IN ()子查询返回competitions.id

可能最容易实现
UPDATE
  fixtures
SET toggle = 'visible'
WHERE 
  /* Modify rows currently hidden (not strictly necessary since they would all end up with the same value anyway) */
  toggle = 'hidden'
  /* Retrieve competitions.id matching your criteria */
  AND compid IN (
   SELECT id 
   FROM `competitions` 
   WHERE `Year` LIKE CONVERT( _utf8 '2012' USING latin1 ) COLLATE latin1_swedish_ci 
      /* IN () clause is equivalent to your chain of OR operations */
      AND countyid IN (4, 11, 20, 22)
  )