【问题标题】:Match all WHERE IN values using Kohana ORM使用 Kohana ORM 匹配所有 WHERE IN 值
【发布时间】:2013-01-05 20:12:57
【问题描述】:

我有以下基于标签获取产品的代码:

self::factory('user_product')
    ->join('items', 'INNER')->on('user_product.item_id', '=', 'items.id')
    ->join('user_tags', 'INNER')->on('items.id', '=', 'user_tags.item_id')
    ->join('tags', 'INNER')->on('user_tags.tag_id', '=', 'tags.id')
    ->where('tags.title', 'IN', $tags)
    ->order_by('items.created_at', 'DESC')->group_by('items.id')->find_all();

生成以下 SQL:

SELECT `user_product`.* FROM `user_products` AS `user_product`
INNER JOIN `items` ON (`user_product`.`item_id` = `items`.`id`) 
INNER JOIN `user_tags` ON (`items`.`id` = `user_tags`.`item_id`)
INNER JOIN `tags` ON (`user_tags`.`tag_id` = `tags`.`id`)
WHERE `tags`.`title` IN ('tag1', 'tag2')
GROUP BY `items`.`id`
ORDER BY `items`.`created_at` DESC

我想修改我的查询,使其仅返回同时具有tag1tag2user_products(在 where in 语句中)。所以我想我需要在WHERE之后添加以下内容:

HAVING COUNT(DISTINCT `tags`.`title`) = 2

我将如何使用 Kohana 的 ORM 来做到这一点,目前我有以下但不知道如何集成 COUNT 和 DISTINCT 方法:

->having_open()->having('tags.title','=','2')->having_close();

【问题讨论】:

    标签: php mysql kohana kohana-orm


    【解决方案1】:

    使用以下方法让它工作:

    ->having_open()->having(DB::expr('COUNT(DISTINCTtags.title)'),'=',2)->having_close();

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-22
      • 2011-11-22
      • 2012-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多