Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6985169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T18:38:45+00:00 2026-05-27T18:38:45+00:00

The Schema: I have 3 Tables: User Feature User_has_Feature: initially all users has no

  • 0

The Schema:

I have 3 Tables:

  • User
  • Feature
  • User_has_Feature:

initially all users has no features

Example data:

User:

| id | name |
| 1  | Rex  |
| 2  | Job  |

Feature:

| id | name |
| 1  | Eat  |
| 2  | Walk |

User_has_Feature:

| id | user_id | feature_id | have_feature |
| 1  | 1       | 1          | true         |
| 2  | 1       | 1          | true         |
| 3  | 2       | 2          | true         |
| 4  | 2       | 2          | false        |

The questions are:

  • ¿How to get only the records that have all features? (explicitly)

Example:

| user_name | feature_name | feature_status |
| Rex       | Eat          | true           |
| Rex       | Walk         | true           |
  • How to get records that do not have all the features? (again explicitly)

Example:

| user_name | feature_name | feature_status |
| Job       | Eat          | true           |
| Job       | Walk         | false          |

Some conditions have to be attended

  • I need the Users list with all features (true or false) in both queries like examples
  • User have 650k records (for now)
  • Feature have 45 records (for now)
  • Is one time query.

The idea is to export the result to a CSV file


Early Solution

thanks to the answers of (@RolandoMySQLDBA, @Tom Ingram, @DRapp) I found a solution:

SELECT u.name, f.name, IF(uhf.status=1,'YES','NO') as status
FROM user u
  JOIN user_has_feature uhf ON u.id = uhf.user_id
  JOIN feature f ON f.id = uhf.feature_id
  JOIN 
       (
         SELECT u.id as id
         FROM user u
           JOIN user_has_feature uhf ON uhf.user_id = u.id
         WHERE uhf.status = 1
         GROUP BY u.id
         HAVING count(u.id) <= (SELECT COUNT(1) FROM feature)
       ) as `condition` ON `condition`.id = u.id
ORDER BY u.name, f.id, uhf.status

For get records that do not have all the features and for get all record that have all features change:

  • WHERE uhf.status = 1 by WHERE uhf.status = 2
  • HAVING count(u.id) <= (SELECT COUNT(1) FROM feature) by HAVING count(u.id) = (SELECT COUNT(1) FROM feature)

but I want to know if this is an optimal solution?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T18:38:46+00:00Added an answer on May 27, 2026 at 6:38 pm
    SELECT
        UNF.*,
        IF(
            (LENGTH(UNF.FeatureList) - LENGTH(REPLACE(UNF.FeatureList,',','')))
            = (FC.FeatureCount - 1),'Has All Features','Does Not Have All Features'
        ) HasAllFeatures
    FROM
        (SELECT
            U.name user_name
            GROUP_CONCAT(F.name) Features
        FROM
            (SELECT user_id,feature_id FROM User_has_Feature
            WHERE feature_status = true) UHF
            INNER JOIN User U ON UHF.user_id = U.id
            INNER JOIN Feature F ON UHF.feature_id = F.id
        GROUP BY
           U.name
        ) UNF,
        (SELECT COUNT(1) FeatureCount FROM Feature) FC
    ;
    

    The UNF subquery returns with all users listed in User_has_Feature and a comma-separated list of the features. The column HasAllFeatures is determined by the number of columns in UNF.FeatureList. In your case, there are two features. If the number of commas in UNF.FeatureList is FeatureCount – 1, then the user has all features. Otherwise, user does not have all features.

    Here is a better version that shows all users and whether or not they have all, some or no features

    SELECT
        U.name user_name,
        IFNULL(UsersAndFeatures.HasAllFeatures,
        'Does Not Have Any Features')
        WhatFeaturesDoesThisUserHave
    FROM
        User U LEFT JOIN
        (
            SELECT
                UHF.user_id id,
                IF(
                    (LENGTH(UHF.FeatureList) - LENGTH(REPLACE(UHF.FeatureList,',','')))
                    = (FC.FeatureCount - 1),
                   'Has All Features',
                   'Does Not Have All Features'
                ) HasAllFeatures
            FROM
                (
                    SELECT user_id,GROUP_CONCAT(Feature.name) FeatureList
                    FROM User_has_Feature INNER JOIN Feature
                    ON User_has_Feature.feature_id = Feature.id
                    GROUP BY user_id
                ) UHF,
                (SELECT COUNT(1) FeatureCount FROM Feature) FC
        ) UsersAndFeatures
    USING (id);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Schema: Users ID Name Password Address ID Street UserID Both tables have an ID
My Schema I have the following tables table notes/example values ------------------------------------------------ users ( id
I have a star schema type data base, with fact tables that have many
I have 3 tables: user , 'user_friends' and 'blog_post' The schema is similar to
I have a tables of Users, Carts, Orders and OrderTransactions. My schema looks like:
I have two tables in my database schema that represent an entity having a
I have two tables userdetails and blog question The schema is UserDetails: connection: doctrine
I have two tables, both named as say, Employee in two different schema HR
I have a set of tables (say Account, Customer) in a schema (say dbo)
In my SQL Server database schema I have a data table with a date

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.