Join 1 Row to Multiple Rows in PDO

Neel

Here is my scenario:

Database Name: Children
+-------------+---------+---------+
| child_id   | name      | user_id |
+-------------+---------+---------+

    1           Beyonce     33
    2           Cher        33
    3           Madonna     33
    4           Eminem      33


Database Name: Parents
+-------------+---------+---------+
| parent_id   | child_id   | parent_name |
+-------------+---------+---------+

    1           1           Obama
    2           1           Michelle
    3           4           50cents
    4           4           Gaga


Desired Output:
+-------------+---------+---------+
| child_id   | name      | parent Name |
+-------------+---------+---------+
    1           Beyonce     Obama (Row 1) Michelle (Row 2)

PHP SQL Query in PDO:

$sql = "SELECT  Children.child_id, Children.name, Parents.parent_name
        FROM Children               
        LEFT JOIN Parents
            ON Children.child_id = Parents.child_id
        WHERE Children.user_id = ?
        ";

$stmt = $db_PDO->prepare($sql); 

if($stmt->execute(array($userId))) // $userId defined earlier
{
        // Loop through the returned results
        $i = 0;

        foreach ($stmt as $row) {
            $fetchArray[$i] = array (
                'childId' => $row['child_id'],
                'childName' => $row['name'],    
                'parentName' => $row['parent_name'],    
                // How do I save the multiple parents from other rows here ????
                );                  
            $i++;
        }             
}

How can I run a query that Joins 1 row to multiple rows in second table in PDO? I have read other topics here but I am unsure. Is it easier to add a second query that gets the linked parents for each child_id separately in a loop? I am worried that will be too much query. Can someone help me solve this?

Tschallacka

Well, took me some fiddling to test it all out but here you go. Unfortunately one cannot easely pivot tables in mysql but there are alternatives.

http://sqlfiddle.com/#!9/1228f/26

SELECT GROUP_CONCAT(
  CONCAT_WS(':', Parents.parent_id,Parents.parent_name) ) FROM Parents where Parents.child_id=1

;
SELECT
  Children.child_id,
  Children.name,
  GROUP_CONCAT(
  CONCAT_WS(':', Parents.parent_id,Parents.parent_name)    ) as parents
FROM
  Children
  LEFT JOIN Parents
        ON Children.child_id = Parents.child_id
        WHERE Children.user_id = 33
  Group by Children.child_id

This query uses the group concat to concatenate all resulsts we want into a colon seperated string with the values we want, and comma's between the individual fields.

We could do some tricky magic to make them individual fields but that would break our php because we wouldnt know how much fields each query would return(adopted, orphan, no known parents, etc...)

In php you could feed them into an object

$parents = array();
$loop1 = explode(',',$row['parents']);
    foreach($loop1 as $parentset) {
        $parentdetail = explode(":",$parentset);// decide yourself how much detail you want in here... I jsut went with name and id.
        $parent = new stdClass();
        $parent->id = $parentdetail[0];
        $parent->name = $parentdetail[1];
        array_push($parents,$parent);
    }

var_dump($parents);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Join 1 Row to Multiple Rows in PDO

From Dev

In temporary tables/variables: join multiple rows to table with only 1 row

From Dev

Pandas: join multiple columns of one row to many rows (1:n)

From Dev

PDO Update 1 column multiple rows with array

From Dev

PDO Update 1 column multiple rows with array

From Dev

SQL join on multiple Rows to Single row

From Dev

Join multiple rows with specific delimiter to one row

From Dev

Join data from rows to 1 row depending on the number of rows

From Dev

JOIN multiple rows to multiple columns in single row Netezza/Postgres

From Dev

TSQL: Displaying multiple rows of results as 1 row

From Dev

SQL combine multiple rows into 1 row

From Dev

MYSQL combine multiple rows into single row using join with another table

From Dev

PostgreSQL select query 'join multiple rows to one row'

From Dev

How to update a single row from multiple rows with UPDATE JOIN

From Dev

MySQL JOIN returning only 1 row out of several rows

From Dev

PHP PDO Multiple Table join

From Dev

Pdo multiple row insert issue

From Dev

Pivoting denormalized rows--converting multiple rows to single row--causes slow LEFT JOIN

From Dev

ORACLE Join table to select multiple record in 1 row

From Dev

PDO : insert 3 rows instead in mysql when i insert just 1 row

From Dev

Talend - generating n multiple rows from 1 row

From Dev

How to delete rows that has multiple values and pick only 1 row

From Dev

MySQL Update 1 row from multiple rows of data with same key

From Dev

MySQL Update 1 row from multiple rows of data with same key

From Dev

Update multiple rows using a case statement only updates 1 row

From Dev

In SqlServer, how to join a single column from multiple rows into a single row with multiple columns

From Dev

Decrement column value on multiple rows with PHP and PDO

From Dev

Updating multiple columns and rows using PDO

From Dev

Selecting multiple rows in a Select statement using PDO

Related Related

  1. 1

    Join 1 Row to Multiple Rows in PDO

  2. 2

    In temporary tables/variables: join multiple rows to table with only 1 row

  3. 3

    Pandas: join multiple columns of one row to many rows (1:n)

  4. 4

    PDO Update 1 column multiple rows with array

  5. 5

    PDO Update 1 column multiple rows with array

  6. 6

    SQL join on multiple Rows to Single row

  7. 7

    Join multiple rows with specific delimiter to one row

  8. 8

    Join data from rows to 1 row depending on the number of rows

  9. 9

    JOIN multiple rows to multiple columns in single row Netezza/Postgres

  10. 10

    TSQL: Displaying multiple rows of results as 1 row

  11. 11

    SQL combine multiple rows into 1 row

  12. 12

    MYSQL combine multiple rows into single row using join with another table

  13. 13

    PostgreSQL select query 'join multiple rows to one row'

  14. 14

    How to update a single row from multiple rows with UPDATE JOIN

  15. 15

    MySQL JOIN returning only 1 row out of several rows

  16. 16

    PHP PDO Multiple Table join

  17. 17

    Pdo multiple row insert issue

  18. 18

    Pivoting denormalized rows--converting multiple rows to single row--causes slow LEFT JOIN

  19. 19

    ORACLE Join table to select multiple record in 1 row

  20. 20

    PDO : insert 3 rows instead in mysql when i insert just 1 row

  21. 21

    Talend - generating n multiple rows from 1 row

  22. 22

    How to delete rows that has multiple values and pick only 1 row

  23. 23

    MySQL Update 1 row from multiple rows of data with same key

  24. 24

    MySQL Update 1 row from multiple rows of data with same key

  25. 25

    Update multiple rows using a case statement only updates 1 row

  26. 26

    In SqlServer, how to join a single column from multiple rows into a single row with multiple columns

  27. 27

    Decrement column value on multiple rows with PHP and PDO

  28. 28

    Updating multiple columns and rows using PDO

  29. 29

    Selecting multiple rows in a Select statement using PDO

HotTag

Archive