DELETE FROM表WHERE条件在其他表中得到满足

时间:2017-08-05 20:04:35

标签: mysql sql

我有这个SQL查询一直很好用。当PRC_FIX中的DESCR列以清除而不是IM_ITEM开头时,我希望有类似的内容从ITEM_VEND_NO = 'GAMES WORK'删除一行。

DELETE `PRC_FIX` FROM `PRC_FIX` 
INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO` 
                     AND `IM_ITEM`.`ITEM_VEND_NO` = 'GAMES WORK'

感谢您的帮助。

编辑:这被标记为可能重复。我不知道查看建议的副本会对我有所帮助,因为我不知道如何在涉及2个表的情景中实现它,但我愿意承认这可能是我的由于我不熟悉SQL而导致错误。

3 个答案:

答案 0 :(得分:2)

您可以使用

# sample data
transaction_id = ["001","002","003","004","005"]
product_id = {"product_id":[("P01",), ("P01", "P02"), ("P01", "P02", "P09"),
                            ("P01", "P03"), ("P01", "P03", "P05")]}
testing_df = pd.DataFrame(product_id, index=transaction_id)
testing_df.index.name = "transaction_id"

print(testing_df)
                     product_id
transaction_id                 
001                      (P01,)
002                  (P01, P02)
003             (P01, P02, P09)
004                  (P01, P03)
005             (P01, P03, P05)

matches = {}

for i, entry in enumerate(testing_df.product_id):

    # ... some computation ...

    transaction_id = testing_df.index[i]
    recommendation = entry[0] # just as an example
    matches[transaction_id] = recommendation

print(matches)
{'001': 'P01', '002': 'P01', '003': 'P01', '004': 'P01', '005': 'P01'}

答案 1 :(得分:2)

您需要使用通配符%。

为了匹配这个字符串,其中不同的字符串以“Clearance”开头,你需要使用“Clearance%”。

请看这里:SQL like search string starts with

你是固定代码:

 DELETE `PRC_FIX` FROM `PRC_FIX` 
    INNER JOIN `IM_ITEM` ON `IM_ITEM`.`ITEM_NO` = `PRC_FIX`.`ITEM_NO` 
                         AND IM_ITEM.DESCR LIKE 'Clearance%'

答案 2 :(得分:2)

object collection {
  object Implicits {
    implicit class RichList[A](private val underlying: List[A]) extends AnyVal {
      def mapNext[U](f: (A, A) => U): List[U] = {
        @tailrec
        def loop(in: List[A], out: List[U]): List[U] = in match {
          case Nil          => out
          case head :: tail => loop(tail, out ::: tail.map { f(head, _) } )
        }

        loop(underlying, Nil)
      }

      def mapEvery[U](step: Int)(f: A => U) = {
        @tailrec
        def loop(in: List[A], out: List[U]): List[U] = {
          in match {
            case Nil => out.reverse
            case head :: tail => loop(tail.drop(step), f(head) :: out)
          }
        }

        loop(underlying, Nil)
      }
      def mapDrop[U](drop1: Int, drop2: Int, step: Int)(f: (A, A) => U): List[U] = {
        @tailrec
        def loop(in: List[A], out: List[U]): List[U] = in match {
          case Nil          => out
          case head :: tail =>
            loop(tail.drop(drop1), out ::: tail.drop(drop2).mapEvery(step) { f(head, _) } )
        }

        loop(underlying, Nil)
      }
    }
  }
}