sql >> データベース >  >> RDS >> Mysql

特定のタグを持つアイテムを検索するためのMySQLクエリ

    2つのタグのいずれかが付いたアイテムが必要な場合は、次のようにします。

    select distinct item_id, item_name 
    from items_tags 
    where tag_name in ('yellow', 'fruit'); 
    

    両方のタグが付いたアイテムが必要な場合:

    select item_id, item_name 
    from items_tags 
    where tag_name in ('yellow', 'fruit')
    group by item_id, item_name
    having count(*) = 2; 
    

    コメントに基づく

      select a.id, a.item 
        from items a, items_tags b, tags c 
       where a.id = b.item_id
         and b.tag_id = c.id
    group by id, item
      having (group_concat(c.tag) like '%yellow%' 
             and  group_concat(c.tag) like '%fruit%')
          or group_concat(c.tag) = 'red';
    

    このクエリは、itemsテーブルからidとitemを提供します。黄色とフルーツの両方のタグが付いたアイテムを提供します。と赤いタグのみのアイテム。

    2つのタグと2つのタグのみを持つアイテムを取得する場合は、次の条件を使用して句を作成します

    (group_concat(c.tag) like '%yellow%' 
    and group_concat(c.tag) like '%fruit%'
    and count(*) = 2) 
        
    


    1. Ldap認証およびデータベースオーソリティへのSpringセキュリティスイッチ

    2. MariaDBデータベースを暗号化された状態と暗号化されていない状態に移動する

    3. 複数のトランザクション中のデータベース行のロック

    4. 値で識別されるOracle12c