php - UNION ALL on a Table Join -
php - UNION ALL on a Table Join -
i learned how utilize union command. however, don't know how utilize join.
can show me how modify query below performs left bring together on 2 tables - gw_articles_usa , gw_articles_canada?
$stm = $pdo->prepare("select wt.n, wt.url, wt.title, usa.url, usa.brief, usa.article, usa.pagedex, usa.links gw_topics wt left bring together gw_articles_usa usa on usa.url = wt.url wt.url = :myurl"); $stm->execute(array( 'myurl'=>$myurl ));
provided usa , canada tables have same column names , types, utilize union in subselect.
select wt.n, wt.url, wt.title, country.url, country.brief, country.article, country.pagedex, country.links gw_topics wt left bring together (select * gw_articles_usa union select * gw_articles_canada) country on country.url = wt.url wt.url = :myurl
if 2 tables don't match, can utilize aliases column names create them match or utilize constant/null missing column value , alias name of other table. if info types don't match, cast or other conversion functions can used create them same. if links missing 1 of tables, can this.
select url, brief, article, pagedex, links gw_articles_usa union select url, brief, article, pagedex, null links gw_articles_canada
this should resolve problem.
php mysql union union-all
Comments
Post a Comment