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

  • Home
  • SEARCH
  • 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 6879531
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T04:51:30+00:00 2026-05-27T04:51:30+00:00

I have a USERS table like this USERS user_id | username 1 tom 2

  • 0

I have a USERS table like this

USERS

user_id | username
1         tom
2         sam

I also have a USER_META table like this

USER_META

user_meta_id | user_id | meta_key | meta_value
1              1         active     1
2              1         car        dodge
3              2         active     0
4              2         car        honda

My problem is that I need to select the meta_keys active and and car but only for users who have a value of 1 for active so my result set should be something like this

user_id | user_name | user_meta_id | meta_key | meta_value
1         tom         1              active     1
1         tom         2              car        dodge

I have tried a few things but I cannot get this type of result set. Here is what I thought might work

SELECT * FROM USERS
LEFT JOIN USER_META 
   ON USERS."user_id" = USER_META 
   AND (USER_META."meta_key" = 'active' OR USER_META."meta_key" = 'car') 
WHERE (USER_META."meta_key" = 'active' 
AND USER_META."meta_value" = 1)

the problem with this is that I get a result set that is missing the car meta_key/meta_value

user_id | user_name | user_meta_id | meta_key | meta_value
1         tom         1              active     1

How can I modify the query to get all of the meta information? thanks.

  • 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-27T04:51:31+00:00Added an answer on May 27, 2026 at 4:51 am

    First off, this sort of entity-attribute-value data model is generally a very poor idea. You’re basically re-implementing what a relational database gives you out of the box and queries get very unwieldy very quickly. Generally, you’ll need to join in the USER_META table N times in order to either get N attributes as columns or in order to have N predicates on your data.

    On the data modeling front, future developers will generally be grateful if you avoid creating case-sensitive column names so that they don’t have to double-quote every identifier every time.

    Since you need two keys, you should be able to do something like this (I assume that meta_value is always stored as a character string even if it represents a numeric or boolean value)

    SELECT usr."user_id",
           usr."user_name",
           meta."user_meta_id",
           meta."meta_key",
           meta."meta_value"
      FROM users usr
           JOIN user_meta meta ON (usr."user_id" = meta."user_id")
     WHERE meta."meta_key" in ('active', 'car')
       AND usr."user_id" IN (SELECT active."user_id"
                               FROM user_meta active
                              WHERE active."meta_key" = 'active'
                                AND active."meta_value" = '1' )
    

    which returns the expected results for the data you posted

    SQL> ed
    Wrote file afiedt.buf
    
      1  with users as (
      2    select 1 "user_id", 'tom' "user_name" from dual union all
      3    select 2, 'sam' from dual
      4  ),
      5  user_meta as (
      6    select 1 "user_meta_id",
      7           1 "user_id",
      8           'active' "meta_key",
      9           '1' "meta_value" from dual union all
     10    select 2, 1, 'car', 'dodge' from dual union all
     11    select 3, 2, 'active', '0' from dual  union all
     12    select 4, 2, 'car', 'honda' from dual
     13  )
     14  SELECT usr."user_id",
     15         usr."user_name",
     16         meta."user_meta_id",
     17         meta."meta_key",
     18         meta."meta_value"
     19    FROM users usr
     20         JOIN user_meta meta ON (usr."user_id" = meta."user_id")
     21   WHERE meta."meta_key" in ('active', 'car')
     22     AND usr."user_id" IN (SELECT active."user_id"
     23                             FROM user_meta active
     24                            WHERE active."meta_key" = 'active'
     25*                             AND active."meta_value" = '1' )
    SQL> /
    
       user_id use user_meta_id meta_k meta_
    ---------- --- ------------ ------ -----
             1 tom            1 active 1
             1 tom            2 car    dodge
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Hi i have database like this Users Table user_id | username | branch_id(foreignkey)| Approved_id(foreignkey)
Say I have a table like this: create table users ( user_id int not
I have a couple of tables which look like this Table 1 user_id |
I have a users like this: id username 1 user1 2 user2 3 user3
Is there a way to query users table like this: | id | username
I have a database with three tables set up like this: Users : username,
I have two tables users(id, username, password) and user_badges(user_id, badge_id, badge_slot) I would like
I have a User table defined like this: CREATE TABLE Users( UserId int IDENTITY(1,1)
I have two tables like of this structure: content (content_id, content_type, user_id, time, comment_count)
I have a table which stores test results like this: user | score |

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.