Jump to content

Complex MySQL Query (inner joins and concats)


jarvis

Recommended Posts

Hi All,

 

Thanks everyone for the help so far, it's been great. I've one final query I can't seem to work out. Here's my tabel structure:

 

wp_bp_groups

id | name

1 | Aerosols

2 | Boats

3 | Boilers

 

wp_bp_groups_members

id | groupd_id | user_id

1 | 1 | 1

2 | 2 | 2

 

wp_bp_xprofile_data

id | field_id | user_id | value

1 | 1 | 1 | john smith

2 | 2 | 1 | john@smith.com

3 | 3 | 1 | 123456

4 | 1 | 2 | joe bloggs

5 | 2 | 2 | joe@bloggs.com

6 | 3 | 2 | 987564

 

What I want to get is:

 

Aerosols

John Smith, john@smith.com, 123456

 

Boats

Joe Bloggs, joe@bloggs.com, 987654

 

I'm at a complete loss with this one.  :'(

 

TIA if you can help in any way!

Link to comment
Share on other sites

I've tried this:

SELECT wp_bp_groups.name,  GROUP_CONCAT( wp_bp_xprofile_data.value SEPARATOR ',' )

FROM wp_bp_xprofile_data, wp_bp_groups_members, wp_bp_groups

WHERE wp_bp_groups.id = wp_bp_groups_members.id

AND wp_bp_groups_members.id = wp_bp_xprofile_data.id

Although it returns a result, it only returns a single result and not:

Aerosols

John Smith, john@smith.com, 123456

 

More like:

Aerosols John

Link to comment
Share on other sites

That's because there is only the one record that matches on the wp_xprofile table under the id field.  You will need to link to the user_id field, and I think that's the problem with the statement I gave you - user_id is what mySQL calls it's own user ID field, and it may be getting mixed up :/  can you rename the field to uid or something similar?

Link to comment
Share on other sites

just out of curiosity, what happens when you run your

SELECT wp_bp_groups.name,  GROUP_CONCAT( wp_bp_xprofile_data.value SEPARATOR ',' )
FROM wp_bp_xprofile_data, wp_bp_groups_members, wp_bp_groups
WHERE wp_bp_groups.id = wp_bp_groups_members.id
AND wp_bp_groups_members.id = wp_bp_xprofile_data.id

with the last = wp_bp_xprofile_data.id changed to = wp_bp_xprofile_data.user_id instead?

Link to comment
Share on other sites

Thanks for the perservering! I get this error:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'wp_bp_groups_members.id = wp_bp_xprofile_data.id LIMIT 0, 30' at line 1

 

Was that what you were expecting?

Link to comment
Share on other sites

Oooooh that seems to be getting somewhere. I now get:

name | value

aerosols | joe bloggs

null | joe@bloggs.com

null | 123456

 

Oddly only shows one category? And the values list down. Changing the query to:

SELECT wp_bp_groups.name, GROUP_CONCAT( wp_bp_xprofile_data.value SEPARATOR ',' )

FROM wp_bp_groups

RIGHT JOIN wp_bp_xprofile_data

ON (wp_bp_groups.id = wp_bp_xprofile_data.user_id)

That sorts the list values. Will give that some more testing. I think it's only returning one person and one category. However, I've several test people assigned to several cats. Will test more!! Thanks

Link to comment
Share on other sites

this a working/tested select based on your data.

 

SELECT wp_bp_groups.name,
      GROUP_CONCAT( wp_bp_xprofile_data.value ORDER BY wp_bp_xprofile_data.field_id )
FROM (wp_bp_xprofile_data
  JOIN wb_bp_groups_members ON wp_bp_xprofile_data.user_id = wb_bp_groups_members.user_id)
  JOIN wp_bp_groups ON wp_bp_groups.id = wb_bp_groups_members.group_id
GROUP BY wp_bp_groups.name;

 

 

Link to comment
Share on other sites

@jarvis:

 

This is the query that I gave to you:

SELECT wp_bp_groups.name,
      GROUP_CONCAT( wp_bp_xprofile_data.value ORDER BY wp_bp_xprofile_data.field_id )
FROM (wp_bp_xprofile_data
  JOIN wb_bp_groups_members ON wp_bp_xprofile_data.user_id = wb_bp_groups_members.user_id)
  JOIN wp_bp_groups ON wp_bp_groups.id = wb_bp_groups_members.group_id
GROUP BY wp_bp_groups.name;

 

and your answer:

Table 'xxx.wb_bp_groups_members' doesn't exist

 

now.. tell me... where in the code that I gave to you is something like this xxx.wb_bp_groups_members>:(

 

and worst... what you did to find the problem and fix YOUR error? ... go back to your code and check it again.

 

 

Link to comment
Share on other sites

Hi Mikosiko,

 

Thanks for the reply. I tried running your code direct in phpmyadmin as a sql query. The error it gave back I displayed but edited out the table database name. I ran the query exactly as you had it I assure you.

 

Apologies as never meant to cause offence, just posting my results/findings :-(

Link to comment
Share on other sites

@jarvis:

 

Don't worry... is not offense (or anything similar)at all... I'm just trying to push you to debug your code by yourself... that is the best way for you to learn.

 

The problem is simple and you should have be able to catch it... your table name is wp_bp_groups_members  and in the code that I gave to you:

SELECT wp_bp_groups.name,
      GROUP_CONCAT( wp_bp_xprofile_data.value ORDER BY wp_bp_xprofile_data.field_id )
FROM (wp_bp_xprofile_data
  JOIN wb_bp_groups_members ON wp_bp_xprofile_data.user_id = wb_bp_groups_members.user_id)
  JOIN wp_bp_groups ON wp_bp_groups.id = wb_bp_groups_members.group_id
GROUP BY wp_bp_groups.name;

 

is named  as wb_bp_groups_members  you see the problem now?...

Link to comment
Share on other sites

At mikosiko. No that's fine. It was one of those couldn't see for looking. Like how many f's in this sentence:

FINISHED FILES ARE THE RE-SULT OF YEARS OF SCIENTIFIC STUDY COMBINED WITH THE EXPERIENCE OF YEARS

 

That code worked like a charm & I'm incredibly thankful. Everyday is a learning curve, some more than others & each day I learn more tricks & tips to try before pestering the forums.

 

Thanks again

 

p.s There are 6

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.