SQL-将LIKE与带有AND&OR运算符的占位符一起使用

时间:2019-03-04 15:28:47

标签: mysql sql sql-like

我正在尝试使用LIKE运算符和通配符来检查[Food]和[Drinks]表各2列中的相似匹配项。

以下是我的解决方案,但无法运行。下面的代码:(为了阅读目的,我在中间留了一个空白)。

" SELECT DISTINCT r.restname"
    + " FROM restaurants r"
    + " JOIN food f ON f.restid = r.restid"
    + " JOIN drinks d ON d.restid = r.restid"

    + " WHERE f.foodcategory LIKE CONCAT('%', ?, '%')" 
    + " OR f.foodname LIKE CONCAT('%', ?, '%')"
    + " AND d.drinkname LIKE CONCAT('%', ?, '%')" 
    + " OR d.drinkvariety LIKE CONCAT('%', ?, '%')");

2 个答案:

答案 0 :(得分:0)

您应该改用exists -就像我在前面的问题中所建议的那样。

但是您的问题是缺少括号:

WHERE (f.foodcategory LIKE CONCAT('%', ?, '%')" OR
       f.foodname LIKE CONCAT('%', ?, '%')
      ) AND
      (d.drinkname LIKE CONCAT('%', ?, '%') OR
       d.drinkvariety LIKE CONCAT('%', ?, '%')
      )

答案 1 :(得分:0)

将您的单词添加到'?并检查。

class MoviesSpider(BaseSpider):

  name = 'movies' #name of the spider
  allowed_domains = ['imdb.com']
  start_url = 'http://imdb.com/list/ls055386972/'

  def __init__(self):
      super(MoviesSpider, self).__init__()

  def start_requests(self):
      yield Request(self.start_url, callback=self.parse, headers=self.headers)

  def parse(self, response):
      #events = response.xpath('//*[@property="url"]/@href').extract()
      links = response.xpath('//h3[@class]/a/@href').extract()

      final_links = []

      for link in links:
          final_link = 'http://www.imdb.com' + link
          final_links.append(final_link)

      for final_link in final_links:
          absolute_url = response.urljoin(final_link)
          yield Request(absolute_url, callback = self.parse_movies)

          #process next page url
          #next_page_url = response.xpath('//a[text() = "Next"]/@href').extract_first()
          #absolute_next_page_url = response.urljoin(next_page_url)
          #yield Request(absolute_next_page_url)

  def parse_movies(self, response):

      title  = response.xpath('//div[@class = "title_wrapper"]/h1[@class]/text()').extract_first()

      yield{
                'title': title,
      }