php - Subquery to see if a field in a separate table is null or empty for the same id -
i getting authors way:
$feat_authors = $wpdb->get_results("select id, user_nicename $wpdb->users display_name <> 'admin' order rand() limit 3");
but want check in seperate database table: wp_usermeta
there field meta_key='description'
see if value is not empty or null associated id (user_id , id first table match).
selecting , printing wp_usermeta
yields:
array ( [0] => stdclass object ( [umeta_id] => 19 [user_id] => 2 [meta_key] => first_name [meta_value] => rita ) [1] => stdclass object ( [umeta_id] => 20 [user_id] => 2 [meta_key] => last_name [meta_value] => santos ) [2] => stdclass object ( [umeta_id] => 21 [user_id] => 2 [meta_key] => nickname [meta_value] => rita ) [3] => stdclass object ( [umeta_id] => 22 [user_id] => 2 [meta_key] => description [meta_value] => i’m coolest person in uniplaces. post stuff on facebook , other social networks. )
and tried this, know gives me empty descriptions: select * wp_usermeta meta_key = 'description' , meta_value = ''
but i'm not sure how combine them in first query results description in second table not empty or null. thing throwing me off i'm not sure how bring id on second query first id has different named (id opposed user_id).
not 100% sure getting asking for, can use join table data , case statement?
select id , user_nicename ,case when len([meta_key='description'])>0 'some content' when len([meta_key='description'])=0 'no content' when [meta_key='description'] null 'null' else 'other' end category $wpdb->users u left outer join wp_usermeta m on u.id=m.user_id display_name <> 'admin' order rand() limit 3
if want see users dont have null or empty descriptions maybe:
select id , user_nicename ,[meta_key='description'] $wpdb->users u inner join wp_usermeta m on u.id=m.user_id display_name <> 'admin' , len([meta_key='description'])>0 , [meta_key='description'] not null -- not required depending on len() behaviour order rand() limit 3
or maybe have multiple records in second table per user , want meta_key = description?
select id , user_nicename ,meta_value $wpdb->users u left outer join wp_usermeta m on u.id=m.user_id , meta_key='description' display_name <> 'admin' , len(meta_value)>0 order rand() limit 3
i had quick @ table structures , think want somthing option 3
Comments
Post a Comment