PDO multiple inner joins appear to be looping

Jamie McAllister

I'm using INNER JOIN for the first time and I'm a little confused.

I have 3 tables:

dog
client
dogclient

I'm trying to get information from both the client and dog tables for every record in the dogclient table. when i do, the result is this:

[{"ID":"1","Name":"Sparky","Forename":"Jamie","Surname":"McAllister"},
{"ID":"1","Name":"Sparky","Forename":"Jamie","Surname":"McAllister"},
{"ID":"2","Name":"Scruff","Forename":"Jamie","Surname":"McAllister"},
{"ID":"2","Name":"Scruff","Forename":"Jamie","Surname":"McAllister"}]

The function i'm using to make this query is:

function getAll(){

    $conn = self::connect();

    $stmt = $conn->prepare("SELECT dog.ID, dog.Name, client.Forename, client.Surname FROM dogclient INNER JOIN client ON dogClient.ClientID INNER JOIN dog ON dogclient.DogID"); 
    $stmt->execute();

    // set the resulting array to associative
    $result = $stmt->setFetchMode(PDO::FETCH_ASSOC); 
    $response = $stmt->fetchAll();

    return $response;

Can anyone see where I've gone wrong?

or, does anyone know a better way to do this?

if it helps, there are only 2 dogs in the dog table, one client in the client table, and 2 records in the dogclient table, so i was expecting 2 results

chaoss88

Try this:

INNER JOIN client ON dogClient.ClientID = client.ClientID INNER JOIN dog ON dogclient.DogID = dog.DogID

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

mySQL multiple inner joins

From Dev

Multiple Inner Joins - MySQL

From Dev

mySQL multiple inner joins

From Dev

SQL Multiple inner joins

From Dev

Multiple inner joins returning Error

From Dev

Multiple inner joins in MySql not working

From Dev

Multiple Inner Joins with Polymorphic Association

From Dev

Inner joins selecting multiple columns

From Dev

How to use multiple inner joins

From Dev

MySQL optimize multiple inner joins

From Dev

Multiple inner joins to get a single record by sorting

From Dev

Get data from multiple inner joins

From Dev

Create multiple INNER JOINS with Sequelize ORM

From Dev

MySQL query with multiple inner joins on same table

From Dev

Create multiple INNER JOINS with Sequelize ORM

From Dev

where condition failed on multiple inner joins in sql

From Dev

How to improve query performance for multiple inner joins?

From Dev

Multiple Inner Joins from same table

From Dev

PDO deleting records from multiple tables using joins

From Dev

LINQ syntax for SQL query with multiple inner joins and aliases

From Dev

SQL Select Statement with multiple INNER JOINS and WHERE conditions

From Dev

MySQL - Display multiple rows in one field (tables with inner joins)q

From Dev

Optimization of multiple inner self-joins by unique key

From Dev

SQL error on multiple joins: "Encountered INNER. Was expecting one of "and"... "except"

From Dev

Getting all records from INNER JOINS of multiple tables

From Dev

PHP PDO with 2 inner joins. How to differentiate between them in php

From Dev

PHP PDO with 2 inner joins. How to differentiate between them in php

From Dev

SQL Group By with Inner Joins

From Dev

Inner joins in Ebean?

Related Related

HotTag

Archive