【问题标题】:using subquery within a where like clause MYSQL在 where like 子句 MYSQL 中使用子查询
【发布时间】:2013-12-09 17:42:00
【问题描述】:

大家好,我有以下疑问:

update tblAnimal as p 
join tblGrouping as tfs on tfs.tblGroupingId = p.tblGroupingId
join tblShelter as ses on ses.tblShelterId = tfs.tblShelterId
join tblFind as tf on ses.tblFindId = tf.tblFindId
set findColor = 'y'
where p.animalData like (select searchName from tblSearchCriteria where animalId =       p.tblanimalId) 
or    p.history like (select searchName from tblSearchCriteria where animalId =    p.tblanimalId);

所以我想这个查询只从子查询中返回一列是可以的,但是一旦您开始从该返回查询中返回多个值,我们就会开始遇到麻烦。

我的问题是,当预期从子查询返回多于一列时,实现上述目标的最佳方法是什么;本质上,我想要做的是处理与搜索条件表中一样多的 searchNames,并将 findcolor 更新为“y”,以匹配任何返回的搜索名称。

我希望这是可以理解的。

【问题讨论】:

  • 在尝试 UPDATE 之前,看看您是否可以编写一个返回所需结果集的 SELECT。
  • searchName 是否包含诸如 % 之类的通配符?
  • 没有,目前它只包含一个要搜索的字符串

标签: mysql join subquery


【解决方案1】:

如果 searchName 包含像 % 这样的通配符,您可以使用 JOIN 重写您的查询:

UPDATE
  tblAnimal as p
  JOIN tblSearchCriteria sc
       ON p.tblanimalId = sc.animalId AND (p.animalData LIKE sc.searchName
                                           OR p.history LIKE sc.searchName)
  JOIN tblGrouping as tfs on tfs.tblGroupingId = p.tblGroupingId
  JOIN tblShelter as ses on ses.tblShelterId = tfs.tblShelterId
  JOIN tblFind as tf on ses.tblFindId = tf.tblFindId
SET
  findColor = 'y'

否则你可以用 IN 代替 LIKE。

【讨论】:

  • 谢谢,事实证明这是重新实现的最佳方式 :)
猜你喜欢
  • 2015-02-18
  • 2020-10-13
  • 1970-01-01
  • 2014-01-17
  • 1970-01-01
  • 1970-01-01
  • 2023-03-13
  • 2018-11-17
  • 1970-01-01
相关资源
最近更新 更多