Forums

Custom mysql query (6 posts)

  1. Asaf050
    Member
    Posted 8 months ago #

    Hey,

    I'm trying to get a slick way of ordering by lastname.
    Here's what I've got so far:
    $author_subscriper = $wpdb->get_results("SELECT * from $wpdb->usermeta");
    Now, how do I do this and sort by last name?

    Thanks!
    Asaf

  2. popper
    Member
    Posted 8 months ago #

    Have your tried ORDER BY last_name?

  3. Asaf050
    Member
    Posted 8 months ago #

    Yes I tried, the problem is that there is no column named "last_name".
    There is meta_key clolum that the value his'last_name' and his 'meta_value" is tha last name.
    for example:
    umeta_id.user_id...meta_key..meta_value
    5...............5........last_name..Jacson
    21.............1........last_name..Weston
    27.............3........first_name.Bill
    30.............3........last_name..Gates

  4. popper
    Member
    Posted 8 months ago #

    You're totally right. Completely forgot how the tables where constructed! Sorry about that. How about something like this?

    SELECT * FROM wp_users INNER JOIN wp_usermeta ON (wp_users.id = wp_usermeta.user_id) WHERE meta_key ="last_name" ORDER BY meta_value

    I don't know exactly what you need, but that'll give you the user fields ordered by the user last name.

  5. Asaf050
    Member
    Posted 8 months ago #

    Thanks for your comment, INNER JOIN isn't needed beacuse I'm currectly using the user meta table.
    This is my query right now:
    $author_subscriper = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'wp_capabilities' AND meta_value = 'a:1:{s:10:\"subscriber\";b:1;}' AND WHERE meta_key ='last_name' ORDER BY 'meta_value'");

    Isn't working for me... (It doesn't show result at all)
    Looking forward for your replay, thanks :)

  6. Asaf050
    Member
    Posted 8 months ago #

    Get it work! thank you very much!
    this is my query now:

    $author_subscriper = $wpdb->get_results("SELECT * from $wpdb->usermeta WHERE meta_key = 'last_name' ORDER BY BINARY meta_value");

Reply

You must log in to post.

About this Topic

Tags

No tags yet.