开发者

changing left join to basic join

开发者 https://www.devze.com 2023-01-13 00:44 出处:网络
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

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_conte开发者_开发知识库nt` 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`


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消