norus Posted August 13, 2015 Share Posted August 13, 2015 In WordPress I need a listing generated from table wp_usermeta which shows following results per row per user_id :user_id, city, first_name+ " "+lastnameThis is an example of the data in table wp_usermeta. 126 city Utrecht126 last_name Huisman126 first_name Fred100 city Eibergen100 last_name Van der Heide100 first_name Toon90 city Hengelo90 last_name Klaver90 first_name Gezien What I need is following result:126 Utrecht Fred Huisman100 Eibergen Toon Van der Heide90 Hengelo Gezien KlaverBelow script is wors. But it give you an idea of the table _ fields:SELECT wp_users.ID, wp_usermeta.meta_key , wp_usermeta.meta_valueFROM wp_users INNER JOIN wp_usermeta ON wp_users.ID = wp_usermeta.user_idWHEREwp_usermeta.meta_key = "city"ORwp_usermeta.meta_key = "first_name" + "last_name"OR(wp_usermeta.meta_key = "lastwill_public" AND wp_usermeta.meta_value = "1")AND wp_usermeta.meta_value is nullORDER BY wp_users.ID DESC Who can help me getting the result.In advance thanks! Quote Link to comment Share on other sites More sharing options...
Barand Posted August 13, 2015 Share Posted August 13, 2015 Something like this will do it. (It isn't pretty, but then neither is the wp_usermeta table). SELECT u.id , firstname.meta_value , lastname.meta_value , city.meta_value FROM wp_users u INNER JOIN ( SELECT u.id , m.meta_value FROM wp_users u LEFT JOIN wp_usermeta m ON u.ID=m.user_id AND m.meta_key = 'firstname' ) firstname USING (id) INNER JOIN ( SELECT u.id , m.meta_value FROM wp_users u LEFT JOIN wp_usermeta m ON u.ID=m.user_id AND m.meta_key = 'lastname' ) lastname USING (id) INNER JOIN ( SELECT u.id , m.meta_value FROM wp_users u LEFT JOIN wp_usermeta m ON u.ID=m.user_id AND m.meta_key = 'city' ) city USING (id) ORDER BY u.id; Quote Link to comment Share on other sites More sharing options...
norus Posted August 15, 2015 Author Share Posted August 15, 2015 I solved this with creating an extra table for addresses. The WP-usermeta table sucks. The idea of Guru was nice but not functional. WIth creating new table the insert + update is very easy and to get the data from the table is also better. Thanks for the reply. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.