简体   繁体   English

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

[英]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. 我想显示服务表status ='A',g1 ='G1'和add_servicess表g1 ='G1',type ='E'标题。这是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> ';
    }
}

服务表

add_services表

but its display same title in 2 times.eg i have column tittle "abc".it's display abc in two times. 但它在2次中显示相同的标题。例如,我有标题为“ abc”的栏目。它在两次中显示abc。

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' SELECT services.id,services.title,services.link,services.link_open,add_services.aid,add_services.tittle。 'E'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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