php - SQL Query based on value of a different row -
i'm trying query data multiple tables cannot seem achieve type of join. example, have 1 table contains products, , wp_usermeta table various user meta data:
products table
serial # product --------------------- 0001 0002 b 0003 c wp_usermeta (simplified)
user id key value -------------------------------- 1 serials 0001 1 company company 2 serials 0001 2 company company b 3 serials 0001 3 company company c i need company name based on serial numbers own, such as:
serial # company ------------------------- 0001 company 0002 company b 0003 company c i've been able retrieve user id based on serial number simple join query, dont know go there.
select products.serial_number, wp_usermeta.user_id products left join wp_usermeta on products.serial_number = wp_usermeta.meta_value
i not sure products table has results need.
one method "simple" self-join:
select p.value serial_number, c.value company.user_id wp_usermeta p join wp_usermeta c on p.user = c.user , p.key = 'serials' , c.key = 'company';
Comments
Post a Comment