Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9226251
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T04:47:05+00:00 2026-06-18T04:47:05+00:00

I, I would like to make a query and sorting my members in a

  • 0

I, I would like to make a query and sorting my members in a special way… Could someone help ?
Here’s the problem.
I would like to select members in my table using a special sort order.
The profile fields values are stored in a table wp_bp_xprofile_data like this :

   id   | field_id | user_id |     value
--------+----------+---------+----------

For example, I have 3 fields

  • NICKNAME (field_id = 1)
  • FIRSTNAME (field_id = 2)
  • LASTNAME (field_id = 3)

The table rows will look like this :

   id   | field_id | user_id |     value
--------+----------+---------+----------
  2544       1          100       fib
  2545       2          100       john
  2546       3          100       arenzich

  2547       1          200       dog
  2548       2          200       rick
  2549       3          200       zarenburg

  2550       1          300       fox
  2551       2          300       frank
  2552       3          300       arenzich

I’ve got this query to sort them using one field, for example to sort them by nickname alphabetically :

SELECT *
FROM wp_bp_xprofile_data u WHERE u.field_id = 1 ORDER BY u.value ASC

So they will be sorted like this : dog(200),fib(100), then fox(300).
Now, I would like to sort them not one but several fields (firstname and lastname; to differenciate people with same lastname) so that the query returns the users in this order :

frank arenzich (300), john arenzich (100), frank arenzich (200).

Any idea for doing this ?

Thanks A LOT !!!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T04:47:06+00:00Added an answer on June 18, 2026 at 4:47 am

    This will probably need to be done by first pivoting this into a proper table by column, and then ordering that on multiple columns.
    Note: this will not produce output in the same format as your original table, but is arguably a lot more flexible and useful as it combines all information about each user_id into a single row.

    /* A column-wise pivot of NICKNAME, FIRSTNAME, LASTNAME */
    SELECT
      user_id,
      MAX(CASE WHEN field_id = 1 THEN value ELSE null END) AS NICKNAME,
      MAX(CASE WHEN field_id = 2 THEN value ELSE null END) AS FIRSTNAME,
      MAX(CASE WHEN field_id = 3 THEN value ELSE null END) AS LASTNAME
    FROM wp_bp_xprofile_data
    GROUP BY user_id
    /* Include the HAVING if you only want those who have both first & last names specified */
    HAVING 
      FIRSTNAME IS NOT NULL 
      AND LASTNAME IS NOT NULL
    /* Pivoted columns can then be treated in the ORDER BY */
    ORDER BY 
      FIRSTNAME,
      LASTNAME
    

    Here is a demonstration…

    It looks like this is a WordPress table, so you may not be in a position to change its structure. But if you do have the option of modifying it, I would recommend changing the structure to resemble the pivot’s output to begin with.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a table with languages. I would like to make a query that
here is my problem, i would like to make twitter like direct message system
I would like to know how to make a single query to select something
Anyone knows a good way to make UNION query in CakePHP? I would like
I would like to make this query in JPA: SELECT * FROM `happyDB`.`users` U
I would like to make a query which needs to compare an property's property
I would like to make a query for database user roles for all databases
I would like to make an SQL query to compare two tables with identical
I would like to make a query that returns the rows that has proper
I would like to make a MySQL query, ordered sequentially by date. How would

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.