MySQL, join many to many relationship query

Niroda

I have this query:

SELECT
    GROUP_CONCAT(DISTINCT
        `persons`.`name`,
        '[START_Name_END]',
        `persons`.`id`,
        '[START_Id_END]',
        `persons`.`isteacher`,
        '[START_IsTeacher_END]'
    SEPARATOR '[START_ROWSEPERATOR_END]') AS persons_values,
    `locations`.`name` AS locations_name,
    `locations`.`id` AS locations_id
FROM `locations`
INNER JOIN `locations_persons` ON `locations_persons`.`location_id` = `locations`.`id`
INNER JOIN `persons` ON `persons`.`id` = `locations_persons`.`person_id`  
GROUP BY `locations`.`id`
ORDER BY `locations`.`id`

The reason why I use GROUP_CONCAT is to get all rows from persons table, that have reference in the association table, in the same column

I use [START_Name_END], [START_Id_END] to split the result later in my code and [START_ROWSEPERATOR_END] to determine if it's same row or a new one.

Everything works just fine so far.

The thing is if any row in locations table has no reference to any row in persons table, I'm NOT getting that row in the results!

I beleive that the problem is in JOIN, what should I use to fetch all rows regardless if that row has reference in the association table or not?

Any help would be greatly appreciated!

Jeff Breadner

Try using:

LEFT OUTER JOIN `locations_persons` ...

-- Edit

Sorry, I'm new at answering these StackOverflow things and didn't give this the attention that it deserves before responding.

Your table structure is a classic many-to-many join, so if there is no person for a given location, then there would only be location data in the locations table. As such, you would need to have a LEFT OUTER JOIN for both the join on locations_persons AND the join from locations_persons to persons.

If you're still having troubles I'll create a quick MySQL database and test things out.. I'll do that from now on, but thought I should reply more fully now because of my bad partial answer earlier.

--Edit2

Regarding expecting issues: When you left outer join, any join that fails (i.e. there's rows on the left side, but no match on the right side) will have the tables from the right returning all null values. In this case, you would want your query to properly handle all fields in locations_persons and persons returning null values.

If GROUP_CONCAT doesn't deal with this well (I'm not familiar with it), you might need to wrap these persons.* fields in an IFNULL, if you're having problems:

SELECT
  GROUP_CONCAT(
    DISTINCT `persons`.`name`,
    '[START_Name_END]',
    IFNULL(`persons`.`id`, ''),
    '[START_Id_END]',
    IFNULL(`persons`.`isteacher`, ''),
    '[START_IsTeacher_END]' SEPARATOR '[START_ROWSEPERATOR_END]'
  ) AS persons_values,
  `locations`.`name` AS locations_name,
  `locations`.`id` AS locations_id
FROM
  `locations`
  LEFT OUTER JOIN `locations_persons` ON
    `locations_persons`.`location_id` = `locations`.`id`
  LEFT OUTER JOIN `persons` ON
    `persons`.`id` = `locations_persons`.`person_id`
GROUP BY
  `locations`.`id`
ORDER BY
  `locations`.`id`

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Many to many relationship and MySQL

From Dev

Mysql Many to Many relationship

From Dev

mysql join query with many fields

From Dev

Mysql Left Join (1 to many relationship)

From Dev

Query many to many relationship with DetachedCriteria

From Dev

how to query a many to many relationship?

From Dev

eloquent: query many to many relationship

From Dev

How to query for many to many relationship between products and filters in MySQL?

From Dev

How to query a pivot table data in MySQL (many to many relationship)

From Dev

php mysql many to many relationship query result display

From Dev

denormalize many to many relationship in MySQL

From Dev

mysql join query taking too many resources

From Dev

SQL Query with Aggregate function on Left Join of One-to-Many Relationship

From Dev

Many to many join Php/mysql

From Dev

JPQL left outer join on many to many relationship

From Dev

Laravel Complicated inner join on many to many relationship

From Dev

Oracle Recursive Join - Many to Many Relationship

From Dev

Inner Join Between Many to Many Relationship Models

From Dev

Ebean many to many relationship with join fails

From Dev

Rails: many to many relationship join table design

From Dev

How to JOIN tables in many-to-many relationship

From Dev

mysql join one to many relationship and print in different rows

From Dev

Sqlalchemy one to many relationship join?

From Dev

Many to one relationship, unwanted JOIN

From Dev

Exclusive join on has many relationship

From Dev

JPA criteria query in a many-to-many relationship

From Dev

Parse - Array of pointers - Many to Many relationship query

From Dev

CoreData: Query to one-to-many-to-many relationship

From Dev

Many-to-many relationship query alright?

Related Related

  1. 1

    Many to many relationship and MySQL

  2. 2

    Mysql Many to Many relationship

  3. 3

    mysql join query with many fields

  4. 4

    Mysql Left Join (1 to many relationship)

  5. 5

    Query many to many relationship with DetachedCriteria

  6. 6

    how to query a many to many relationship?

  7. 7

    eloquent: query many to many relationship

  8. 8

    How to query for many to many relationship between products and filters in MySQL?

  9. 9

    How to query a pivot table data in MySQL (many to many relationship)

  10. 10

    php mysql many to many relationship query result display

  11. 11

    denormalize many to many relationship in MySQL

  12. 12

    mysql join query taking too many resources

  13. 13

    SQL Query with Aggregate function on Left Join of One-to-Many Relationship

  14. 14

    Many to many join Php/mysql

  15. 15

    JPQL left outer join on many to many relationship

  16. 16

    Laravel Complicated inner join on many to many relationship

  17. 17

    Oracle Recursive Join - Many to Many Relationship

  18. 18

    Inner Join Between Many to Many Relationship Models

  19. 19

    Ebean many to many relationship with join fails

  20. 20

    Rails: many to many relationship join table design

  21. 21

    How to JOIN tables in many-to-many relationship

  22. 22

    mysql join one to many relationship and print in different rows

  23. 23

    Sqlalchemy one to many relationship join?

  24. 24

    Many to one relationship, unwanted JOIN

  25. 25

    Exclusive join on has many relationship

  26. 26

    JPA criteria query in a many-to-many relationship

  27. 27

    Parse - Array of pointers - Many to Many relationship query

  28. 28

    CoreData: Query to one-to-many-to-many relationship

  29. 29

    Many-to-many relationship query alright?

HotTag

Archive