简体   繁体   中英

Join One Column From other Table and Echo it

I have two table called

teacher_profile which have fields like id, centerId, name, study, class etc And

table center which have column like id, name, location.

I am trying to get all fields from table teacher_profile and want center name from center table with that query.

$qry="select * from teacher_profile AS q 
left join center AS a on a.id = q.centerId
ORDER BY q.id DESC";

$result=mysqli_query($mysqli,$qry);

Its giving me all column from table center as well. I want only name column from it as centerName so I can display it properly in my PHP table. Let me know if someone can help me for achieve it. Thanks

You can use q.* and a.center uisng alias center_name

    $qry="select q.*, a.center center_name from teacher_profile AS q 
            left join center AS a on a.id = q.centerId
           ORDER BY q.id DESC";
        $result=mysqli_query($mysqli,$qry);

you can refere the the column using index center_name

       echo $row['center_name'];

On the query, select statement you can specify which columns you want.

see example

$qry="
SELECT
   center.name, teacher_profile.*
FROM 
    teacher_profile
left join center on teacher_profile.id = center.centerId
ORDER BY 
    teacher_profile.id DESC
";
$result=mysqli_query($mysqli,$qry);

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