sql >> Database >  >> RDS >> Mysql

Compex MySQL Left Join met behulp van meerdere items uit metatabellen

Plaats de gebruikersmeta-lidmaatschapscriteria in de join in plaats van te worstelen met subquery's:

SELECT
    ID, user_email, user_login, 
    first_name.meta_value as first_name,
    last_name.meta_value as last_name,
    phone_number.meta_value as phone_number,
    wp_capabilities.meta_value as wp_capabilities 
FROM wp_users
    JOIN wp_usermeta AS wp_capabilities ON wp_capabilities.user_id=ID
        AND wp_capabilities.meta_key='wp_capabilities'
    LEFT JOIN wp_usermeta AS first_name ON first_name.user_id=ID
        AND first_name.meta_key='first_name'
    LEFT JOIN wp_usermeta AS last_name ON last_name.user_id=ID
        AND last_name.meta_key='last_name'
    LEFT JOIN wp_usermeta AS phone_number ON phone_number.user_id=ID
        AND phone_number.meta_key='phone_number'
WHERE
    wp_capabilities.meta_value LIKE '%administrator%'
ORDER BY
    first_name



  1. 15 handige tips voor het afstemmen en optimaliseren van MySQL/MariaDB-prestaties

  2. Een lokale SQL Server-database instellen

  3. Json naar SQL-tabel converteren

  4. MySQL Tijd van oud record ophalen?