简体   繁体   中英

How to select values from two tables in mysql

I want to display services table status='A', g1='G1'and add_servicess table g1='G1', type='E' titles.this is mysql query.

$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> ';
    }
}

服务表

add_services表

but its display same title in 2 times.eg i have column tittle "abc".it's display abc in two times.

Shouldn't your query be:

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'

You can use this query....

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM