久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

如何匹配與一組特定的其他記錄相關(guān)聯(lián)的記錄?

How to match records that are associated with a specific set of other records?(如何匹配與一組特定的其他記錄相關(guān)聯(lián)的記錄?)
本文介紹了如何匹配與一組特定的其他記錄相關(guān)聯(lián)的記錄?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

我正在嘗試向我的項(xiàng)目添加兩個(gè)不同的搜索變體.有一個(gè)模型用戶(hù)"和一個(gè)模型標(biāo)簽".一個(gè)用戶(hù)有很多標(biāo)簽.現(xiàn)在我希望能夠搜索具有特定標(biāo)簽的用戶(hù).我想顯示所有具有任何指定標(biāo)簽的用戶(hù).我是這樣工作的:

I am trying to add to two different search variations to my project. There is a model "User" and a model "Tag". A User has many Tags. Now I want to be able to search the Users with specific Tags. Either I want to show all Users that has any of the specified tags. I got this working this way:

$query = $this->Users->find();
$query->matching('Tags', function ($q) {
    return $q->where(['Tags.name' => 'Tag1'])
             ->orWhere(['Tags.name' => 'Tag2']);
});

但現(xiàn)在我想找到同時(shí)擁有兩個(gè)標(biāo)簽的所有用戶(hù).我試過(guò) ->andWhere 而不是 ->orWhere,但結(jié)果總是空的.

But now I want to find all Users that have both Tags at the same time. I tried ->andWhere instead of ->orWhere, but the result is always empty.

如何找到包含多個(gè)標(biāo)簽的用戶(hù)?

How can I find Users that contain multiple Tags?

謝謝

推薦答案

有幾種方法可以實(shí)現(xiàn)這一點(diǎn),一種是將結(jié)果分組并使用 HAVING 來(lái)比較不同的計(jì)數(shù)標(biāo)簽

There are a few ways to achieve this, one would be to group the results and use HAVING to compare the count of the distinct tags

$query = $this->Users
    ->find()
    ->matching('Tags', function ($query) {
        return $query->where(['Tags.name IN' => ['Tag1', 'Tag2']]);
    })
    ->group('Users.id')
    ->having([
        $this->Users->query()->newExpr('COUNT(DISTINCT Tags.name) = 2')
    ]);

這將僅選擇具有兩個(gè)不同標(biāo)簽的用戶(hù),它們只能是 Tag1Tag2,因?yàn)樗鼈兪俏ㄒ槐患尤氲挠脩?hù).以防萬(wàn)一name 列是唯一的,您可以改為依賴(lài)主鍵.

This will select only those users that have two distinct tags, which can only be Tag1 and Tag2 since these are the only ones that are being joined in. In case the name column is unique, you may count on the primary key instead.

IN 順便說(shuō)一句.本質(zhì)上與您的 OR 條件相同(數(shù)據(jù)庫(kù)系統(tǒng)會(huì)相應(yīng)地將 IN 擴(kuò)展為 OR 條件).

The IN btw. is essentially the same as your OR conditions (the database system will expand IN to OR conditions accordingly).

這篇關(guān)于如何匹配與一組特定的其他記錄相關(guān)聯(lián)的記錄?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請(qǐng)聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Session is lost after an OAuth redirect(OAuth 重定向后會(huì)話丟失)
Pagination Sort in Cakephp 3.x(Cakephp 3.x 中的分頁(yè)排序)
CakePHP Shared core for multiple apps(CakePHP 多個(gè)應(yīng)用程序的共享核心)
Login [ Auth-gt;identify() ] always false on CakePHP 3(在 CakePHP 3 上登錄 [ Auth-identify() ] 始終為 false)
Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 87 bytes)(致命錯(cuò)誤:允許的內(nèi)存大小為 134217728 字節(jié)已用盡(嘗試分配 87 字節(jié)))
主站蜘蛛池模板: 另类a v| 精品欧美 | 成人性视频在线 | 黄色在线免费观看 | 一区二区日韩 | 91p在线观看 | 欧美二级 | 91成人在线视频 | 久久久精品视频免费看 | 中文字幕久久精品 | 欧美在线| 91视频一88av | 免费激情网站 | 国产成人久久av免费高清密臂 | 日韩中文在线观看 | 在线观看成年视频 | 亚洲精品成人在线 | 女女百合av大片一区二区三区九县 | 精区3d动漫一品二品精区 | 国产高清免费 | 久久久高清 | 久久综合一区二区 | 国产女人与拘做受视频 | 黄色av免费网站 | 亚洲欧美综合 | 亚洲视频一区在线观看 | 91精品国产色综合久久 | 超碰人人91 | 最新日韩在线视频 | 欧美亚洲国产日韩 | 久久亚洲天堂 | 欧美一区二区 | 精品日韩一区二区 | 亚洲综合色自拍一区 | 久久久影院 | 欧美性区 | 精品一区二区免费视频 | 99热这里有精品 | 亚洲色欧美另类 | 波多野结衣先锋影音 | 黄色国产视频 |