1

I have a table called user_meta. In that table I have the following columns: ID, userID, meta_key, meta_value

I have another table called users, the only important column there is ID, which I want to compare to the user_meta table rows.

The users table looks like:

ID    |    email   | etc...
1     |    [email protected]  |
5     |    [email protected]   |
6     |    ....   |
7     |    ....   |

So say I have a table (user_meta) that looks like:

ID   |   userID   |   meta_key  |   meta_value
2    |   1        |   companyID |   2
3    |   1        |   user_type |   staff
4    |   5        |   companyID |   2
5    |   5        |   user_type |   staff
6    |   6        |   companyID |   4
7    |   6        |   user_type |   customer

I want to retrieve a single row for each userID, but only if the company ID and user_type are correct.

I want to retrieve all users that have the same companyID that I would send in the query, so let's say $companyID=2, and then all users that have the user_type='staff'.

So user_meta.userID must equal users.ID, and user_meta.companyID must equal 2, and user_meta.user_type must equal 'staff'.

I want a list of all users that match these criteria.

A result would be userID 1 & 5 are returned. They both have companyID = 2, and both have user_type = staff

4
  • provide the table structure of both this tables and also the desired result came from your example datas Commented Oct 28, 2016 at 1:55
  • @NewbeeDev I added the users table, the only important column is ID. users.ID = user_meta.userID Commented Oct 28, 2016 at 1:58
  • perhaps a result your desired query would help us understand your problem more. Commented Oct 28, 2016 at 2:04
  • @NewbeeDev added an expected result at the bottom. Commented Oct 28, 2016 at 2:07

3 Answers 3

5

You need to join with user_meta once for each attribute you want to match.

SELECT u.*
FROM users AS u
JOIN user_meta AS m1 ON u.id = m1.userID
JOIN user_meta AS m2 ON u.id = m2.userID
WHERE m1.meta_key = 'companyID' AND m1.meta_value = :companyID
AND m2.meta_key = 'user_type' AND m2.meta_value = 'staff'
Sign up to request clarification or add additional context in comments.

1 Comment

This seems to be working how I wanted.. I will finish coding to make sure. Thank you!
0
SELECT `users`.`id`, 
       `Company`.`meta_value`, 
       `UserType`.`meta_value` 
FROM   `users` 
       JOIN `user_meta` `Company` 
         ON `Company`.`userid` = `users`.`id` 
       JOIN `user_meta` `UserType` 
         ON `UserType`.`userid` = `users`.`id` 
WHERE  `UserType`.`meta_value` = 'staff' 
       AND `Company`.`meta_value` = 2 

https://gyazo.com/de8d9124418f65b993d708c80c309325

Comments

-1

Not very sure about your question. I'm assuming this is what you may want:

select * from Users where ID in (
select userID from user_meta where (meta_key = 'companyID' and meta_value = 2) or (meta_key = 'user_type' and meta_value = 'staff')
);

4 Comments

This will get everyone who is either in company 2 or is staff, it doesn't require them to be both.
@Barmar Ah yes you are right. I didn't get the question right
@PremRaj yes thank you for your attempt, I know the question is a bit confusing.
It's actually a pretty common request, and wrong answers like this are also common.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.