如何从mysql中的两个表中选择值

时间:2014-10-13 12:04:27

标签: php mysql

我想显示服务表状态=' A',g1 =' G1'和add_servicess表g1 =' G1',type =' E' titles.this是mysql查询。

$services_sql="SELECT services.id, services.title,services.link,services.link_open,add_services.aid,add_services.tittle
FROM   services
LEFT JOIN add_services
ON  services.status='A' AND services.g1='G1' AND add_services.g1='G1' AND add_services.type='E'
";
$result = db::getInstance()->query($services_sql);
/*** loop over the results ***/
$chklogin_val = $result->rowCount();
foreach($result as $row) {
    if($acc_status=="A") {
        echo '<div class="servicebox">

        <div class="s_title"><h3>'.$row["title"].'</h3></div>

        <div class="s_img" ><a href="'.$row["link"].'" target="'.$row["link_open"].'" ><img width="'.$_SESSION['img_width'].'" height="'.$_SESSION['img_height'].'" src="service/'.$row["id"].'.jpg" /></a></div>
        </div> ';
        echo '<div class="servicebox">
        <div class="s_title"><a href="page.php?id='.$row['aid'].'" target="_blank"><h3>'.$row["tittle"].'</a></h3></div>
        </div> ';
    }
}

services table

add_services table

但它在2次显示相同的标题。我有列tittle&#34; abc&#34; .it显示abc两次。

2 个答案:

答案 0 :(得分:0)

您的查询不应该是:

SELECT services.id, services.title, services.link, services.link_open, add_services.aid, add_services.tittle
FROM   services
LEFT JOIN add_services ON  AND services.g1 = add_services.g1 AND 
WHERE services.status='A' and add_services.type='E' and services.g1 = 'G1'

答案 1 :(得分:0)

您可以使用此查询....

SELECT services.id,services.title,services.link,services.link_open,add_services.aid,add_services.tittle 来自服务 LEFT JOIN add_services ON services.g1 = add_services.g1其中services.status =&#39; A&#39;和add_services.type =&#39; E&#39;