Domanda

We work on a google map where each user is show with markers.

We have a input field, where the visitor can search for an user. We want to use an autocompletion plugin (coolautosugest) for autocomplete the name of user.

The plugin need a SQL request to get the name. There an example of the table where the user name are saved :

field_id | user_id | value
1        | 2       | Doe
35       | 2       | John
1        | 14      | Obama
35       | 14      | Barack

Where field_id 1 is the last name, and the field_id 35 is the first name

What we need for the autocompletion plugin:

user_id | name
2       | John Doe
14      | Barack Obama

The SQL request tha we have for now is :

SELECT user_id, value FROM wp_sokatira_bp_xprofile_data WHERE field_id=35 OR field_id=1

And the result is :

user_id | value
2       | Doe
2       | John
14      | Obama
14      | Barack

Does someone better than us with SQL have a solution for our problem/challenge ?

Thank you,

Benjamin Dubé (and three other classmates)

È stato utile?

Soluzione 2

Try this:

SELECT user_id, GROUP_CONCAT(value ORDER BY field_id DESC SEPARATOR ' ') as name
FROM wp_sokatira_bp_xprofile_data 
WHERE field_id=35 OR field_id=1
GROUP BY user_id

A suggestion from my side:

You can use IN in where clause:

SELECT user_id, GROUP_CONCAT(value ORDER BY field_id DESC SEPARATOR ' ') as name
FROM wp_sokatira_bp_xprofile_data 
WHERE field_id IN (35,1)
GROUP BY user_id

EDIT:

Use LIKE as follows:

SELECT user_id, GROUP_CONCAT(value ORDER BY field_id DESC SEPARATOR ' ') as name
FROM wp_sokatira_bp_xprofile_data 
WHERE field_id IN (35,1) AND ColName LIKE %$test%
GROUP BY user_id

Parameterize the variable $test.

Altri suggerimenti

You can use GROUP_CONCAT function. Also I don't know why you have separated first and last names but since that wasn't a question, here is a simple example of query

SELECT user_id, GROUP_CONCAT(value ORDER BY field_id DESC SEPARATOR ' ') AS value
FROM `wp_sokatira_bp_xprofile_data`
GROUP BY user_id
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top