MYSQL Select from tables based on multiple rows

后端 未结 3 704
余生分开走
余生分开走 2020-12-02 01:10

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

3条回答
  •  醉酒成梦
    2020-12-02 02:07

    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'
    

提交回复
热议问题