views:

44

answers:

1

Hi there, I currently have some SQL that should return 3 rows of data but returns 6 (3 rows repeated twice).

I believe this is down to my syntax and want to try and build the query using basic joins, currently the SQL looks like this,

`function getMultiContentById($id) {
    $query = "SELECT
    FROM `mailers`
    LEFT JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id`
    LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id`
    WHERE `mailers`.`id` = $id"
    $result = runSelectArray($query, __FUNCTION__);
    return $result;
}`

I want to use something like this

`WHERE `mailer_content`.id = `mailers.id`
+1  A: 

Just change the LEFT to INNER on the first join, as in

$query = "SELECT 
    FROM `mailers` 
    INNER JOIN `mailer_content` ON `mailers`.`id` = `mailer_content`.`mailer_id` 
    LEFT JOIN `mailer_images` ON `mailer_content`.`id` = `mailer_images`.`content_id` 
    WHERE `mailers`.`id` = $id" 
    $result = runSelectArray($query, __FUNCTION__); 
    return $result; 

Share and enjoy.

Bob Jarvis